1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.groovy.support.classes;
35
36 import groovy.lang.GroovyResourceLoader;
37
38 import info.magnolia.cms.core.Content;
39 import info.magnolia.cms.core.HierarchyManager;
40 import info.magnolia.cms.core.NodeData;
41 import info.magnolia.jcr.util.NodeTypes;
42 import info.magnolia.module.groovy.support.HierarchyManagerProvider;
43
44 import java.io.ByteArrayInputStream;
45 import java.io.IOException;
46 import java.io.InputStream;
47 import java.net.MalformedURLException;
48 import java.net.URL;
49 import java.net.URLConnection;
50 import java.net.URLStreamHandler;
51 import java.util.Calendar;
52
53 import javax.jcr.RepositoryException;
54
55
56
57
58
59
60
61
62
63
64
65
66 class MgnlGroovyResourceLoader implements GroovyResourceLoader {
67 private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MgnlGroovyResourceLoader.class);
68
69 private static final String PROTOCOL = "magnolia-repository://";
70 private final GroovyResourceLoader delegate;
71 private final HierarchyManagerProvider hmp;
72 private final String repoName;
73
74 MgnlGroovyResourceLoader(GroovyResourceLoader delegate, HierarchyManagerProvider hmp, String repoName) {
75 this.delegate = delegate;
76 this.hmp = hmp;
77 this.repoName = repoName;
78 }
79
80 @Override
81 public URL loadGroovySource(String name) throws MalformedURLException {
82 final URL url = loadGroovySourceFromRepository(name);
83 if (url == null) {
84 return delegate.loadGroovySource(name);
85 }
86 return url;
87 }
88
89
90
91
92
93 private URL loadGroovySourceFromRepository(String name) throws MalformedURLException {
94 if (name.startsWith("[") || name.contains("$")) {
95
96
97 log.debug("Skipping {}, array or nested class.", name);
98 return null;
99 }
100
101 final HierarchyManager hm;
102 try {
103 hm = hmp.getHierarchyManager(repoName);
104 } catch (Exception e) {
105 return null;
106 }
107 final String path = "/" + name.replace('.', '/');
108 try {
109 if (hm.isExist(path) && hm.getNodeData(path + "/enabled").getBoolean()) {
110 return new URL(null, PROTOCOL + hm.getName() + path, new MagnoliaStreamHandler(hm));
111 } else {
112 return null;
113 }
114 } catch (RepositoryException e) {
115 return null;
116 }
117 }
118
119
120
121
122
123 protected static class MagnoliaStreamHandler extends URLStreamHandler {
124 private final HierarchyManager hm;
125
126 public MagnoliaStreamHandler(HierarchyManager hm) {
127 this.hm = hm;
128 }
129
130 @Override
131 protected URLConnection openConnection(URL u) throws IOException {
132 if (!"magnolia-repository".equals(u.getProtocol())) {
133 throw new IllegalStateException("Unsupported protocol: " + u.getProtocol() + " (only supports \"magnolia-repository\")");
134 }
135 return new MagnoliaURLConnection(u, hm);
136 }
137 }
138
139
140
141
142
143 protected static class MagnoliaURLConnection extends URLConnection {
144 private final Content node;
145
146 public MagnoliaURLConnection(URL u, HierarchyManager hm) throws IOException {
147 super(u);
148
149 final String path = u.getPath();
150
151 try {
152 this.node = hm.getContent(path);
153 } catch (RepositoryException e) {
154 throw new IOException("Can't get " + path + " from the " + hm.getName() + " repository: " + e.getClass().getSimpleName() + ": " + e.getMessage());
155 }
156 }
157
158 @Override
159 public void connect() throws IOException {
160
161
162
163
164
165 }
166
167 @Override
168 public InputStream getInputStream() throws IOException {
169 final NodeData prop = node.getNodeData("text");
170 if (!prop.isExist()) {
171 throw new IOException(node.getHandle() + " has no 'text' property");
172 }
173
174
175
176 final String sourceStr = prop.getString();
177 return new ByteArrayInputStream(sourceStr.getBytes());
178 }
179
180 @Override
181 public long getLastModified() {
182 try {
183 Calendar date = NodeTypes.LastModified.getLastModified(node.getJCRNode());
184 return date.getTimeInMillis();
185 } catch (RepositoryException e) {
186 return 0;
187 }
188
189 }
190 }
191 }