Lines 71-79
Link Here
|
71 |
private static final Logger LOG = Logger.getLogger(RecognizeInstanceFiles.class.getName()); |
71 |
private static final Logger LOG = Logger.getLogger(RecognizeInstanceFiles.class.getName()); |
72 |
|
72 |
|
73 |
|
73 |
|
|
|
74 |
@Override |
74 |
public Lookup create(String path) { |
75 |
public Lookup create(String path) { |
75 |
return new OverFiles(path); |
76 |
return new OverFiles(path); |
76 |
} |
77 |
} |
|
|
78 |
@Override |
79 |
public <T> T lookupObject(String path, Class<T> type) { |
80 |
FileObject fo = FileUtil.getConfigFile(path); |
81 |
if (fo != null && fo.isData()) { |
82 |
Object res = FOItem.createInstanceFor(fo, Object.class); |
83 |
return type.isInstance(res) ? type.cast(res) : null; |
84 |
} |
85 |
return null; |
86 |
} |
77 |
|
87 |
|
78 |
|
88 |
|
79 |
private static final class OverFiles extends ProxyLookup |
89 |
private static final class OverFiles extends ProxyLookup |
Lines 216-236
Link Here
|
216 |
public synchronized Object getInstance() { |
226 |
public synchronized Object getInstance() { |
217 |
Object r = ref.get(); |
227 |
Object r = ref.get(); |
218 |
if (r == null) { |
228 |
if (r == null) { |
219 |
r = fo.getAttribute("instanceCreate"); |
229 |
r = createInstanceFor(fo, Object.class); |
220 |
if (r == null) { |
|
|
221 |
try { |
222 |
Class<?> type = getType(); |
223 |
if (SharedClassObject.class.isAssignableFrom(type)) { |
224 |
r = SharedClassObject.findObject(type.asSubclass(SharedClassObject.class), true); |
225 |
} else { |
226 |
r = type.newInstance(); |
227 |
} |
228 |
} catch (InstantiationException ex) { |
229 |
Exceptions.printStackTrace(ex); |
230 |
} catch (IllegalAccessException ex) { |
231 |
Exceptions.printStackTrace(ex); |
232 |
} |
233 |
} |
234 |
if (r != null) { |
230 |
if (r != null) { |
235 |
ref = new WeakReference<Object>(r); |
231 |
ref = new WeakReference<Object>(r); |
236 |
} |
232 |
} |
Lines 238-250
Link Here
|
238 |
return r; |
234 |
return r; |
239 |
} |
235 |
} |
240 |
|
236 |
|
|
|
237 |
static <T> T createInstanceFor(FileObject f, Class<T> resultType) { |
238 |
Object r = f.getAttribute("instanceCreate"); |
239 |
if (r == null) { |
240 |
try { |
241 |
Class<?> type = findTypeFor(f); |
242 |
if (SharedClassObject.class.isAssignableFrom(type)) { |
243 |
r = SharedClassObject.findObject(type.asSubclass(SharedClassObject.class), true); |
244 |
} else { |
245 |
r = type.newInstance(); |
246 |
} |
247 |
} catch (InstantiationException ex) { |
248 |
Exceptions.printStackTrace(ex); |
249 |
} catch (IllegalAccessException ex) { |
250 |
Exceptions.printStackTrace(ex); |
251 |
} |
252 |
} |
253 |
return resultType.isInstance(r) ? resultType.cast(r) : null; |
254 |
} |
255 |
|
241 |
public Class<? extends Object> getType() { |
256 |
public Class<? extends Object> getType() { |
|
|
257 |
return findTypeFor(fo); |
258 |
} |
259 |
|
260 |
private static Class<? extends Object> findTypeFor(FileObject f) { |
242 |
ClassLoader l = Lookup.getDefault().lookup(ClassLoader.class); |
261 |
ClassLoader l = Lookup.getDefault().lookup(ClassLoader.class); |
243 |
if (l == null) { |
262 |
if (l == null) { |
244 |
l = FOItem.class.getClassLoader(); |
263 |
l = FOItem.class.getClassLoader(); |
245 |
} |
264 |
} |
246 |
try { |
265 |
try { |
247 |
return Class.forName(getClassName(fo), false, l); |
266 |
return Class.forName(getClassName(f), false, l); |
248 |
} catch (ClassNotFoundException ex) { |
267 |
} catch (ClassNotFoundException ex) { |
249 |
LOG.log(Level.INFO, ex.getMessage(), ex); |
268 |
LOG.log(Level.INFO, ex.getMessage(), ex); |
250 |
return Object.class; |
269 |
return Object.class; |