Revision 44831 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.file/org.gvsig.fmap.dal.file.lib/src/main/java/org/gvsig/fmap/dal/FileHelper.java
FileHelper.java | ||
---|---|---|
23 | 23 |
*/ |
24 | 24 |
package org.gvsig.fmap.dal; |
25 | 25 |
|
26 |
import java.io.IOException; |
|
26 | 27 |
import java.io.InputStream; |
28 |
import java.net.URL; |
|
29 |
import org.apache.commons.io.IOUtils; |
|
27 | 30 |
|
28 | 31 |
import org.gvsig.metadata.MetadataLocator; |
29 | 32 |
import org.gvsig.metadata.MetadataManager; |
... | ... | |
45 | 48 |
} |
46 | 49 |
|
47 | 50 |
public static DynStruct registerParametersDefinition(String name, Class theClass, String filename) { |
48 |
DynStruct definition = null; |
|
49 |
|
|
50 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
|
51 |
definition = manager.getDefinition(name); |
|
52 |
if ( definition == null) { |
|
53 |
InputStream stream = theClass.getResourceAsStream(filename); |
|
54 |
if( stream == null ) { |
|
55 |
throw new DefinitionNotFoundException(theClass,filename); |
|
56 |
} |
|
57 |
definition = manager.addDefinition( |
|
58 |
theClass, |
|
59 |
name, |
|
60 |
stream, |
|
61 |
theClass.getClassLoader(), |
|
62 |
null, |
|
63 |
null |
|
64 |
); |
|
65 |
} |
|
66 |
return definition; |
|
51 |
ClassLoader loader = theClass.getClassLoader(); |
|
52 |
URL resource = theClass.getResource(filename); |
|
53 |
return registerParametersDefinition(name, theClass, loader, resource); |
|
54 |
} |
|
55 |
|
|
56 |
public static DynStruct registerParametersDefinition(String name, Class theClass, ClassLoader loader, URL resource) { |
|
57 |
if( name == null ) { |
|
58 |
throw new IllegalArgumentException("Argument 'name' can't be null."); |
|
59 |
} |
|
60 |
if( theClass == null ) { |
|
61 |
throw new IllegalArgumentException("Argument 'theClass' can't be null."); |
|
62 |
} |
|
63 |
if( resource == null ) { |
|
64 |
throw new IllegalArgumentException("Argument 'resource' can't be null."); |
|
65 |
} |
|
66 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
|
67 |
DynStruct definition = manager.getDefinition(name); |
|
68 |
if ( definition == null) { |
|
69 |
InputStream stream = null; |
|
70 |
try { |
|
71 |
stream = resource.openStream(); |
|
72 |
definition = manager.addDefinition( |
|
73 |
theClass, |
|
74 |
name, |
|
75 |
stream, |
|
76 |
loader, |
|
77 |
null, |
|
78 |
null |
|
79 |
); |
|
80 |
} catch (IOException ex) { |
|
81 |
throw new DefinitionNotFoundException(theClass, resource.toString(),ex); |
|
82 |
} finally { |
|
83 |
IOUtils.closeQuietly(stream); |
|
84 |
} |
|
85 |
} |
|
86 |
return definition; |
|
67 | 87 |
} |
68 |
|
|
69 | 88 |
public static DynStruct registerMetadataDefinition(String name, Class theClass, String filename) throws MetadataException { |
70 |
DynStruct definition = null; |
|
71 |
|
|
72 |
MetadataManager manager = MetadataLocator.getMetadataManager(); |
|
73 |
definition = manager.getDefinition(name); |
|
74 |
if ( definition == null) { |
|
75 |
InputStream stream = theClass.getResourceAsStream(filename); |
|
76 |
if( stream == null ) { |
|
77 |
throw new DefinitionNotFoundException(theClass,filename); |
|
78 |
} |
|
79 |
definition = manager.addDefinition( |
|
80 |
name, |
|
81 |
theClass.getResourceAsStream(filename), |
|
82 |
theClass.getClassLoader() |
|
83 |
); |
|
84 |
} |
|
85 |
return definition; |
|
89 |
ClassLoader loader = theClass.getClassLoader(); |
|
90 |
URL resource = theClass.getResource(filename); |
|
91 |
return registerMetadataDefinition(name, theClass, loader, resource); |
|
92 |
} |
|
93 |
|
|
94 |
public static DynStruct registerMetadataDefinition(String name, Class theClass, ClassLoader loader, URL resource) throws MetadataException { |
|
95 |
if( name == null ) { |
|
96 |
throw new IllegalArgumentException("Argument 'name' can't be null."); |
|
97 |
} |
|
98 |
if( theClass == null ) { |
|
99 |
throw new IllegalArgumentException("Argument 'theClass' can't be null."); |
|
100 |
} |
|
101 |
if( resource == null ) { |
|
102 |
throw new IllegalArgumentException("Argument 'resource' can't be null."); |
|
103 |
} |
|
104 |
MetadataManager manager = MetadataLocator.getMetadataManager(); |
|
105 |
DynStruct definition = manager.getDefinition(name); |
|
106 |
if ( definition == null) { |
|
107 |
InputStream stream = null; |
|
108 |
try { |
|
109 |
stream = resource.openStream(); |
|
110 |
definition = manager.addDefinition( |
|
111 |
name, |
|
112 |
stream, |
|
113 |
theClass.getClassLoader() |
|
114 |
); |
|
115 |
} catch (IOException ex) { |
|
116 |
throw new DefinitionNotFoundException(theClass, resource.toString(),ex); |
|
117 |
} finally { |
|
118 |
IOUtils.closeQuietly(stream); |
|
119 |
} |
|
120 |
} |
|
121 |
return definition; |
|
86 | 122 |
} |
87 | 123 |
|
88 | 124 |
public static DynObject newParameters(String name) { |
... | ... | |
109 | 145 |
*/ |
110 | 146 |
private static final long serialVersionUID = 7598155353307119897L; |
111 | 147 |
|
112 |
public DefinitionNotFoundException(Class theClass, String filename ) { |
|
148 |
public DefinitionNotFoundException(Class theClass, String filename ) { |
|
149 |
this(theClass,filename, null); |
|
150 |
} |
|
151 |
|
|
152 |
public DefinitionNotFoundException(Class theClass, String filename, Throwable ex ) { |
|
113 | 153 |
super( |
114 | 154 |
"Can't open parameters definition '%(filename)' from class %(classname).", |
115 | 155 |
"_ResourceForParametersDefinitionNotFoundException", |
116 | 156 |
serialVersionUID |
117 | 157 |
); |
158 |
this.initCause(ex); |
|
118 | 159 |
this.setValue("filename", filename); |
119 | 160 |
this.setValue("classname", theClass.getClass().getName()); |
120 | 161 |
} |
Also available in: Unified diff