Revision 4427
Added by daigle about 16 years ago
src/edu/ucsb/nceas/metacat/SchemaLocationResolver.java | ||
---|---|---|
30 | 30 |
|
31 | 31 |
import java.sql.*; |
32 | 32 |
import java.io.File; |
33 |
import java.io.Reader; |
|
34 |
import java.io.BufferedReader; |
|
35 | 33 |
import java.io.BufferedInputStream; |
36 | 34 |
import java.io.FileWriter; |
37 | 35 |
import java.io.BufferedWriter; |
38 | 36 |
import java.io.InputStream; |
39 | 37 |
import java.io.IOException; |
40 |
import java.net.URL; |
|
41 |
import java.net.URLConnection; |
|
42 |
import java.net.MalformedURLException; |
|
43 |
import java.util.Vector; |
|
44 | 38 |
|
45 | 39 |
import org.apache.log4j.Logger; |
46 | 40 |
|
41 |
import edu.ucsb.nceas.metacat.service.XMLSchemaService; |
|
47 | 42 |
import edu.ucsb.nceas.metacat.util.SystemUtil; |
48 |
import edu.ucsb.nceas.utilities.PropertyNotFoundException; |
|
49 | 43 |
|
50 | 44 |
/** |
51 | 45 |
* A database aware Class to handle schema location. If namespace is in the |
... | ... | |
59 | 53 |
|
60 | 54 |
private String nameSpace = null; //public id |
61 | 55 |
private String schemaLocation = null; // system id |
62 |
private Vector nameSpaceList = new Vector();// name space in the table
|
|
63 |
private String nameSpaceAndLocationString = null; // the parameter will be set
|
|
64 |
// in external property in parser |
|
56 |
// private Vector<String> nameSpaceList = new Vector<String>();// name space in the table
|
|
57 |
// private String nameSpaceAndLocationString = ""; // the parameter will be set
|
|
58 |
// // in external property in parser
|
|
65 | 59 |
//private String schemaFileName = null; |
66 | 60 |
// constant |
67 | 61 |
//private String SCHEMATYPE ="Schema"; |
... | ... | |
71 | 65 |
private static Logger logMetacat = Logger.getLogger(SchemaLocationResolver.class); |
72 | 66 |
|
73 | 67 |
public SchemaLocationResolver() |
74 |
{ |
|
75 |
createNameSpaceAndLocationStringFromDB(); |
|
68 |
{ |
|
76 | 69 |
} |
70 |
|
|
77 | 71 |
/** |
78 | 72 |
* Construct an instance of the SchemaLocationResolver class |
79 | 73 |
* |
... | ... | |
82 | 76 |
*/ |
83 | 77 |
public SchemaLocationResolver(String myNameSpaceAndLocation) |
84 | 78 |
{ |
85 |
parse(myNameSpaceAndLocation); |
|
79 |
parse(myNameSpaceAndLocation); |
|
80 |
|
|
86 | 81 |
} |
87 |
|
|
82 |
|
|
88 | 83 |
/** |
89 |
* A method to get nameSpaceAndLocationString |
|
84 |
* Construct an instance of the SchemaLocationResolver class |
|
85 |
* |
|
86 |
* @param myNameSpaceAndLocation it is come from xsi:schemaLocation= |
|
87 |
* "namespace location" |
|
90 | 88 |
*/ |
91 |
public String getNameSpaceAndLocationString()
|
|
89 |
public SchemaLocationResolver(String myNameSpace, String myLocation)
|
|
92 | 90 |
{ |
93 |
logMetacat.info("namespace and location list is: " + |
|
94 |
nameSpaceAndLocationString); |
|
95 |
return nameSpaceAndLocationString; |
|
91 |
nameSpace = myNameSpace; |
|
92 |
schemaLocation = myLocation; |
|
96 | 93 |
} |
97 | 94 |
|
98 | 95 |
/** |
... | ... | |
101 | 98 |
*/ |
102 | 99 |
public void resolveNameSpace () |
103 | 100 |
{ |
104 |
//get name space lists |
|
105 |
createNameSpaceListFromDB(); |
|
106 | 101 |
|
107 | 102 |
// if name space is not in table |
108 | 103 |
if (nameSpace !=null && schemaLocation != null && |
109 |
!nameSpaceList.contains(nameSpace))
|
|
104 |
!XMLSchemaService.getNameSpaceList().contains(nameSpace))
|
|
110 | 105 |
{ |
111 | 106 |
try |
112 | 107 |
{ |
... | ... | |
115 | 110 |
InputStream in = DBEntityResolver.checkURLConnection(schemaLocation); |
116 | 111 |
|
117 | 112 |
String newURLInMetacat = uploadSchemaFromURL(in); |
113 |
XMLSchemaService.refresh(); |
|
118 | 114 |
|
119 |
// reigister schema |
|
120 |
registerSchema(newURLInMetacat); |
|
115 |
// check the name space list again. It may not have appeared the first time |
|
116 |
// because the schema was in the db but there was no file on disk. If that's |
|
117 |
// the case, it will show up in the name space list now. If it doesn't we need |
|
118 |
// to register the schema in the database. |
|
119 |
if (!XMLSchemaService.getNameSpaceList().contains(nameSpace)) { |
|
120 |
registerSchema(newURLInMetacat); |
|
121 |
XMLSchemaService.refresh(); |
|
122 |
} |
|
121 | 123 |
downloadNewSchema = true; |
122 | 124 |
|
123 | 125 |
} |
... | ... | |
131 | 133 |
|
132 | 134 |
} |
133 | 135 |
|
134 |
/* |
|
135 |
* This method will take look xml_catalog table and create a string look |
|
136 |
* like "namespace1 location namespacke2 location". This string will be set |
|
137 |
* as a extenalschemalocation in SAX parser. Then when parser check the it |
|
138 |
* will ignore the schema location specified by xml document |
|
139 |
*/ |
|
140 |
private void createNameSpaceAndLocationStringFromDB() { |
|
141 |
DBConnection conn = null; |
|
142 |
int serialNumber = -1; |
|
143 |
PreparedStatement pstmt = null; |
|
144 |
ResultSet result = null; |
|
145 |
String sql = "SELECT public_id, system_id FROM xml_catalog where " |
|
146 |
+ "entry_type ='" + DocumentImpl.SCHEMA + "'"; |
|
147 |
try { |
|
148 |
// check out DBConnection |
|
149 |
conn = DBConnectionPool.getDBConnection("SchemaLocationResl.createName"); |
|
150 |
serialNumber = conn.getCheckOutSerialNumber(); |
|
151 |
pstmt = conn.prepareStatement(sql); |
|
152 |
pstmt.execute(); |
|
153 |
result = pstmt.getResultSet(); |
|
154 |
|
|
155 |
// Get string for namespace and location form DB |
|
156 |
boolean hasRow = result.next(); |
|
157 |
StringBuffer buffer = new StringBuffer(); |
|
158 |
boolean firstTime = true; |
|
159 |
while (hasRow) { |
|
160 |
String namespace = result.getString(1); |
|
161 |
String location = result.getString(2); |
|
162 |
// system id may not have server url on front. Add it if not. |
|
163 |
if (!location.startsWith("http://")) { |
|
164 |
try { |
|
165 |
location = SystemUtil.getContextURL() + location; |
|
166 |
} catch (PropertyNotFoundException pnfe) { |
|
167 |
throw new SQLException("Could not find property: " |
|
168 |
+ pnfe.getMessage()); |
|
169 |
} |
|
170 |
} |
|
171 |
String pairOfNameSpaceAndLocation = namespace + WHITESPACESTRING |
|
172 |
+ location; |
|
173 |
if (!firstTime) { |
|
174 |
buffer.append(WHITESPACESTRING); |
|
175 |
} |
|
176 |
buffer.append(pairOfNameSpaceAndLocation); |
|
177 |
firstTime = false; |
|
178 |
hasRow = result.next(); |
|
179 |
|
|
180 |
} |
|
181 |
// give the buffer to the string if buffer has something in it |
|
182 |
if (buffer.length() != 0) { |
|
183 |
nameSpaceAndLocationString = buffer.toString(); |
|
184 |
} |
|
185 |
result.close(); |
|
186 |
pstmt.close(); |
|
187 |
} catch (SQLException e) { |
|
188 |
logMetacat.error("schemaLocation.createNameSpaceAndLocationString: " |
|
189 |
+ e.getMessage()); |
|
190 |
} finally { |
|
191 |
try { |
|
192 |
pstmt.close(); |
|
193 |
}// try |
|
194 |
catch (SQLException sqlE) { |
|
195 |
logMetacat.error("Error in schema.createNameSpaceAndLocation " |
|
196 |
+ sqlE.getMessage()); |
|
197 |
}// catch |
|
198 |
DBConnectionPool.returnDBConnection(conn, serialNumber); |
|
199 |
}// finally |
|
200 |
|
|
201 |
} |
|
202 |
|
|
203 |
/* |
|
204 |
* This method will take look xml_catalog table and create a name space |
|
205 |
* vector already in the table |
|
206 |
*/ |
|
207 |
private void createNameSpaceListFromDB() |
|
208 |
{ |
|
209 |
DBConnection conn = null; |
|
210 |
int serialNumber = -1; |
|
211 |
PreparedStatement pstmt = null; |
|
212 |
ResultSet result = null; |
|
213 |
String sql = "SELECT public_id FROM xml_catalog where " + |
|
214 |
"entry_type ='" + DocumentImpl.SCHEMA +"'"; |
|
215 |
try |
|
216 |
{ |
|
217 |
//check out DBConnection |
|
218 |
conn=DBConnectionPool.getDBConnection("SchemaLocationResl.createNameList"); |
|
219 |
serialNumber=conn.getCheckOutSerialNumber(); |
|
220 |
pstmt = conn.prepareStatement(sql); |
|
221 |
pstmt.execute(); |
|
222 |
result = pstmt.getResultSet(); |
|
223 |
|
|
224 |
// Get string for namespace form DB and store in the vector |
|
225 |
boolean hasRow = result.next(); |
|
226 |
while (hasRow) |
|
227 |
{ |
|
228 |
String namespace = result.getString(1); |
|
229 |
nameSpaceList.addElement(namespace); |
|
230 |
hasRow = result.next(); |
|
231 |
|
|
232 |
} |
|
233 |
pstmt.close(); |
|
234 |
result.close(); |
|
235 |
} |
|
236 |
catch (SQLException e) |
|
237 |
{ |
|
238 |
logMetacat.error("schemaLocation.createNameSpaceList: " + |
|
239 |
e.getMessage()); |
|
240 |
} |
|
241 |
finally |
|
242 |
{ |
|
243 |
try |
|
244 |
{ |
|
245 |
pstmt.close(); |
|
246 |
}//try |
|
247 |
catch (SQLException sqlE) |
|
248 |
{ |
|
249 |
logMetacat.error("Error in schemaLocation.createNameSpaceList: " |
|
250 |
+sqlE.getMessage()); |
|
251 |
}//catch |
|
252 |
DBConnectionPool.returnDBConnection(conn, serialNumber); |
|
253 |
}//finally |
|
254 |
|
|
255 |
} |
|
256 |
|
|
257 |
|
|
258 |
|
|
259 | 136 |
/** |
260 | 137 |
* Upload new Schema located at outside URL to Metacat file system |
261 | 138 |
*/ |
262 | 139 |
private String uploadSchemaFromURL(InputStream istream) throws Exception |
263 | 140 |
{ |
264 | 141 |
|
265 |
String schemaPath = SystemUtil.getContextDir() + "/schema/"; |
|
266 |
String schemaURL = SystemUtil.getContextURL() + "/schema/"; |
|
142 |
String relativeSchemaPath = XMLSchemaService.SCHEMA_DIR; |
|
143 |
String fullSchemaPath = SystemUtil.getContextDir() + relativeSchemaPath; |
|
144 |
String schemaURL = SystemUtil.getContextURL() + relativeSchemaPath; |
|
267 | 145 |
|
268 | 146 |
// get filename from systemId |
269 |
String filename = schemaLocation;
|
|
147 |
String filename = XMLSchemaService.getSchemaFileNameFromUri(schemaLocation);
|
|
270 | 148 |
|
271 | 149 |
if (filename != null && !(filename.trim()).equals("")) |
272 | 150 |
{ |
... | ... | |
287 | 165 |
BufferedInputStream in = new BufferedInputStream(istream); |
288 | 166 |
|
289 | 167 |
// open file writer to write the input into it |
290 |
File f = new File(schemaPath, filename);
|
|
168 |
File f = new File(fullSchemaPath, filename);
|
|
291 | 169 |
synchronized (f) |
292 | 170 |
{ |
293 | 171 |
try |
... | ... | |
329 | 207 |
} |
330 | 208 |
logMetacat.warn("new schema location is: " + schemaURL + |
331 | 209 |
filename); |
332 |
return schemaURL + filename;
|
|
210 |
return relativeSchemaPath + filename;
|
|
333 | 211 |
} |
334 | 212 |
|
335 | 213 |
|
... | ... | |
340 | 218 |
{ |
341 | 219 |
// check systemid is not null |
342 | 220 |
if (systemId == null || nameSpace == null || (nameSpace.trim()).equals("")) |
343 |
{ |
|
344 |
|
|
221 |
{ |
|
345 | 222 |
return; |
346 | 223 |
} |
347 | 224 |
|
... | ... | |
383 | 260 |
+sqlE.getMessage()); |
384 | 261 |
}//catch |
385 | 262 |
DBConnectionPool.returnDBConnection(conn, serialNumber); |
263 |
|
|
386 | 264 |
}//finally |
387 |
|
|
265 |
XMLSchemaService.populateRegisteredSchemaList(); |
|
388 | 266 |
} |
389 | 267 |
|
390 | 268 |
/* |
... | ... | |
420 | 298 |
// Print out a empty schema list |
421 | 299 |
SchemaLocationResolver schema = new SchemaLocationResolver(); |
422 | 300 |
logMetacat.warn("Namespace and Location String: "+ |
423 |
schema.getNameSpaceAndLocationString());
|
|
301 |
XMLSchemaService.getNameSpaceAndLocationString());
|
|
424 | 302 |
// input a schemalocation |
425 | 303 |
SchemaLocationResolver schema2 = new SchemaLocationResolver( |
426 | 304 |
"eml://ecoinformatics.org/eml-2.0.0 " + |
... | ... | |
434 | 312 |
// print out new schema list in db |
435 | 313 |
SchemaLocationResolver schema4 = new SchemaLocationResolver(); |
436 | 314 |
logMetacat.warn("Namespace and Location String: "+ |
437 |
schema4.getNameSpaceAndLocationString());
|
|
315 |
XMLSchemaService.getNameSpaceAndLocationString());
|
|
438 | 316 |
} |
439 | 317 |
catch(Exception e) |
440 | 318 |
{ |
Also available in: Unified diff
Use XMLSchemaService to access persistent schema information.