-
Notifications
You must be signed in to change notification settings - Fork 8
/
InternalResource.java
283 lines (261 loc) · 9.8 KB
/
InternalResource.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sling.installer.core.impl;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import org.apache.felix.cm.file.ConfigurationHandler;
import org.apache.sling.installer.api.InstallableResource;
/**
* Internal resource is a private data object which wraps
* an installable resource and is used to create a registered
* resource.
*
* An internal resource has always:
* - a resource type
* - a digest
*
*/
public class InternalResource extends InstallableResource {
/**
* Create an internal resource.
* @throws IOException if something is wrong
*/
public static InternalResource create(
final String scheme,
final InstallableResource resource)
throws IOException {
// installable resource has an id, a priority and either
// an input stream or a dictionary
InputStream is = resource.getInputStream();
Dictionary<String, Object> dict = resource.getDictionary();
// Handle deprecated types and map them to new types
String type = resource.getType();
if ( InstallableResource.TYPE_BUNDLE.equals(type) ) {
type = InstallableResource.TYPE_FILE;
} else if ( InstallableResource.TYPE_CONFIG.equals(type) ) {
type = InstallableResource.TYPE_PROPERTIES;
}
// check for optional uri (only if type is file and digest is available)
final String resourceUri = (dict != null
&& (type == null || InstallableResource.TYPE_FILE.equals(type))
&& resource.getDigest() != null
&& resource.getDigest().length() > 0) ?
(String)dict.get(InstallableResource.RESOURCE_URI_HINT) : null;
// check if resourceUri is accessible
boolean useResourceUri = resourceUri != null;
if ( resourceUri != null ) {
InputStream resourceUriIS = null;
try {
final URI uri = new URI(resourceUri);
resourceUriIS = uri.toURL().openStream();
// everything fine
} catch (final Exception use) {
useResourceUri = false;
} finally {
if ( resourceUriIS != null ) {
try {
resourceUriIS.close();
} catch (final IOException ignore) {
// ignore
}
}
}
}
if ( is != null &&
(InstallableResource.TYPE_PROPERTIES.equals(type) ||
((type == null || InstallableResource.TYPE_FILE.equals(type)) && isConfigExtension(resource.getId())))) {
try {
dict = readDictionary(is, getExtension(resource.getId()));
} catch (final IOException ioe) {
throw (IOException)new IOException("Unable to read dictionary from input stream: " + resource.getId()).initCause(ioe);
}
is = null;
useResourceUri = false;
}
File dataFile = null;
final String digest;
if ( is == null ) {
// if input stream is null, properties is expected!
type = (type != null ? type : InstallableResource.TYPE_PROPERTIES);
// we always compute a digest
digest = FileDataStore.computeDigest(dict);
} else {
type = (type != null ? type : InstallableResource.TYPE_FILE);
if ( resourceUri != null && useResourceUri ) {
digest = resource.getDigest();
} else {
final String url = scheme + ':' + resource.getId();
// if input stream is not null, file is expected!
dataFile = FileDataStore.SHARED.createNewDataFile(is,
url,
resource.getDigest(),
resource.getType());
if (resource.getDigest() != null && resource.getDigest().length() > 0) {
digest = resource.getDigest();
} else {
digest = FileDataStore.computeDigest(dataFile);
FileDataStore.SHARED.updateDigestCache(url, dataFile, digest);
}
}
}
return new InternalResource(scheme,
resource.getId(),
is,
dict,
type,
digest,
resource.getPriority(),
dataFile,
useResourceUri ? resourceUri : null);
}
/** The unique resource url. */
private final String url;
/** The data file (if copied) */
private File dataFile;
/** The resource uri */
private final String resourceUri;
public InternalResource(
final String scheme,
final String id,
final InputStream is,
final Dictionary<String, Object> dict,
final String type,
final String digest,
final Integer priority,
final File dataFile,
final String resourceUri) {
super(id, is, dict, digest, type, priority);
this.url = scheme + ':' + id;
this.dataFile = dataFile;
this.resourceUri = resourceUri;
}
/** The unique url of the resource. */
public String getURL() {
return this.url;
}
/**
* Copy given Dictionary
*/
public Dictionary<String, Object> getPrivateCopyOfDictionary() {
final Dictionary<String, Object> d = this.getDictionary();
if ( d == null ) {
return null;
}
final Dictionary<String, Object> result = new Hashtable<String, Object>();
final Enumeration<String> e = d.keys();
while(e.hasMoreElements()) {
final String key = e.nextElement();
result.put(key, d.get(key));
}
return result;
}
/**
* Copy the given file and return it.
*/
public File getPrivateCopyOfFile() {
return this.dataFile;
}
/**
* Set the data file.
*/
public void setPrivateCopyOfFile(final File file) {
this.dataFile = file;
}
/**
* Return the resource uri (or null)
*/
public String getResourceUri() {
return this.resourceUri;
}
/**
* Read dictionary from an input stream.
* We use the same logic as Apache Felix FileInstall here:
* - *.cfg files are treated as property files
* - *.config files are handled by the Apache Felix ConfigAdmin file reader
* @param is
* @param extension
* @throws IOException
*/
private static Dictionary<String, Object> readDictionary(
final InputStream is, final String extension)
throws IOException {
final Hashtable<String, Object> ht = new Hashtable<String, Object>();
final BufferedInputStream in = new BufferedInputStream(is);
try {
if ( !extension.equals("config") ) {
final Properties p = new Properties();
in.mark(1);
boolean isXml = in.read() == '<';
in.reset();
if (isXml) {
p.loadFromXML(in);
} else {
p.load(in);
}
final Enumeration<Object> i = p.keys();
while ( i.hasMoreElements() ) {
final Object key = i.nextElement();
ht.put(key.toString(), p.get(key));
}
} else {
// check for initial comment line
in.mark(256);
final int firstChar = in.read();
if ( firstChar == '#' ) {
int b;
while ((b = in.read()) != '\n' ) {
if ( b == -1 ) {
throw new IOException("Unable to read configuration.");
}
}
} else {
in.reset();
}
@SuppressWarnings("unchecked")
final Dictionary<String, Object> config = ConfigurationHandler.read(in);
final Enumeration<String> i = config.keys();
while ( i.hasMoreElements() ) {
final String key = i.nextElement();
ht.put(key, config.get(key));
}
}
} finally {
try { in.close(); } catch (IOException ignore) {}
}
return ht;
}
private static boolean isConfigExtension(final String url) {
final String ext = getExtension(url);
return "config".equals(ext) || "properties".equals(ext) || "cfg".equals(ext);
}
/**
* Compute the extension
*/
private static String getExtension(String url) {
final int pos = url.lastIndexOf('.');
return (pos < 0 ? "" : url.substring(pos+1));
}
}