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 import info.magnolia.cms.core.Content;
38 import info.magnolia.cms.core.HierarchyManager;
39 import info.magnolia.cms.core.MetaData;
40 import info.magnolia.cms.core.NodeData;
41 import info.magnolia.module.groovy.support.HierarchyManagerProvider;
42
43 import javax.jcr.RepositoryException;
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
54
55
56
57
58
59
60
61
62
63
64 class MgnlGroovyResourceLoader implements GroovyResourceLoader {
65 private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MgnlGroovyResourceLoader.class);
66
67 private static final String PROTOCOL = "magnolia-repository://";
68 private final GroovyResourceLoader delegate;
69 private final HierarchyManagerProvider hmp;
70 private final String repoName;
71
72 MgnlGroovyResourceLoader(GroovyResourceLoader delegate, HierarchyManagerProvider hmp, String repoName) {
73 this.delegate = delegate;
74 this.hmp = hmp;
75 this.repoName = repoName;
76 }
77
78 public URL loadGroovySource(String name) throws MalformedURLException {
79 final URL url = loadGroovySourceFromRepository(name);
80 if (url == null) {
81 return delegate.loadGroovySource(name);
82 }
83 return url;
84 }
85
86
87
88
89
90 private URL loadGroovySourceFromRepository(String name) throws MalformedURLException {
91 if (name.startsWith("[") || name.contains("$")) {
92
93
94 log.debug("Skipping {}, array or nested class.", name);
95 return null;
96 }
97
98 final HierarchyManager hm = hmp.getHierarchyManager(repoName);
99 final String path = "/" + name.replace('.', '/');
100 try {
101 if (hm.isExist(path) && hm.getNodeData(path + "/enabled").getBoolean()) {
102 return new URL(null, PROTOCOL + hm.getName() + path, new MagnoliaStreamHandler(hm));
103 } else {
104 return null;
105 }
106 } catch (RepositoryException e) {
107 return null;
108 }
109 }
110
111
112
113
114
115 protected static class MagnoliaStreamHandler extends URLStreamHandler {
116 private final HierarchyManager hm;
117
118 public MagnoliaStreamHandler(HierarchyManager hm) {
119 this.hm = hm;
120 }
121
122 protected URLConnection openConnection(URL u) throws IOException {
123 if (!"magnolia-repository".equals(u.getProtocol())) {
124 throw new IllegalStateException("Unsupported protocol: " + u.getProtocol() + " (only supports \"magnolia-repository\")");
125 }
126 return new MagnoliaURLConnection(u, hm);
127 }
128 }
129
130
131
132
133
134 protected static class MagnoliaURLConnection extends URLConnection {
135 private final Content node;
136
137 public MagnoliaURLConnection(URL u, HierarchyManager hm) throws IOException {
138 super(u);
139
140 final String path = u.getPath();
141
142 try {
143 this.node = hm.getContent(path);
144 } catch (RepositoryException e) {
145 throw new IOException("Can't get " + path + " from the " + hm.getName() + " repository: " + e.getClass().getSimpleName() + ": " + e.getMessage());
146 }
147 }
148
149 public void connect() throws IOException {
150
151
152
153
154
155 }
156
157 public InputStream getInputStream() throws IOException {
158 final NodeData prop = node.getNodeData("text");
159 if (!prop.isExist()) {
160 throw new IOException(node.getHandle() + " has no 'text' property");
161 }
162
163
164
165 final String sourceStr = prop.getString();
166 return new ByteArrayInputStream(sourceStr.getBytes());
167 }
168
169 public long getLastModified() {
170 final MetaData md = node.getMetaData();
171 final Calendar date = md.getModificationDate();
172 return date.getTimeInMillis();
173 }
174 }
175 }