svn-gvsig-desktop / 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 @ 44831
History | View | Annotate | Download (6.55 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.dal; |
25 |
|
26 |
import java.io.IOException; |
27 |
import java.io.InputStream; |
28 |
import java.net.URL; |
29 |
import org.apache.commons.io.IOUtils; |
30 |
|
31 |
import org.gvsig.metadata.MetadataLocator; |
32 |
import org.gvsig.metadata.MetadataManager; |
33 |
import org.gvsig.metadata.exceptions.MetadataException; |
34 |
import org.gvsig.tools.ToolsLocator; |
35 |
import org.gvsig.tools.dynobject.DynObject; |
36 |
import org.gvsig.tools.dynobject.DynStruct; |
37 |
import org.gvsig.tools.exception.BaseRuntimeException; |
38 |
import org.gvsig.tools.persistence.PersistenceManager; |
39 |
import org.slf4j.Logger; |
40 |
import org.slf4j.LoggerFactory; |
41 |
|
42 |
public class FileHelper { |
43 |
|
44 |
private static final Logger LOG = LoggerFactory.getLogger(FileHelper.class); |
45 |
|
46 |
private FileHelper() {
|
47 |
// Do nothing
|
48 |
} |
49 |
|
50 |
public static DynStruct registerParametersDefinition(String name, Class theClass, String filename) { |
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;
|
87 |
} |
88 |
public static DynStruct registerMetadataDefinition(String name, Class theClass, String filename) throws MetadataException { |
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;
|
122 |
} |
123 |
|
124 |
public static DynObject newParameters(String name) { |
125 |
return ToolsLocator.getDynObjectManager()
|
126 |
.createDynObject( |
127 |
ToolsLocator.getPersistenceManager().getDefinition(name) |
128 |
); |
129 |
} |
130 |
|
131 |
public static DynObject newMetadataContainer(String name) { |
132 |
try {
|
133 |
MetadataManager manager = MetadataLocator.getMetadataManager(); |
134 |
DynStruct definition = manager.getDefinition(name); |
135 |
return ToolsLocator.getDynObjectManager().createDynObject(definition);
|
136 |
} catch(NullPointerException e) { |
137 |
throw new CantCreateMetadataException(name,e); |
138 |
} |
139 |
} |
140 |
|
141 |
private static class DefinitionNotFoundException extends BaseRuntimeException { |
142 |
|
143 |
/**
|
144 |
*
|
145 |
*/
|
146 |
private static final long serialVersionUID = 7598155353307119897L; |
147 |
|
148 |
public DefinitionNotFoundException(Class theClass, String filename ) { |
149 |
this(theClass,filename, null); |
150 |
} |
151 |
|
152 |
public DefinitionNotFoundException(Class theClass, String filename, Throwable ex ) { |
153 |
super(
|
154 |
"Can't open parameters definition '%(filename)' from class %(classname).",
|
155 |
"_ResourceForParametersDefinitionNotFoundException",
|
156 |
serialVersionUID |
157 |
); |
158 |
this.initCause(ex);
|
159 |
this.setValue("filename", filename); |
160 |
this.setValue("classname", theClass.getClass().getName()); |
161 |
} |
162 |
} |
163 |
|
164 |
private static class CantCreateMetadataException extends RuntimeException { |
165 |
|
166 |
/**
|
167 |
*
|
168 |
*/
|
169 |
private static final long serialVersionUID = -8373306339537106075L; |
170 |
|
171 |
CantCreateMetadataException(String name, Throwable cause) { |
172 |
super("Can't retrieve metadata definition for '"+name+"'.", cause); |
173 |
} |
174 |
} |
175 |
} |