1
2
3
4
5
6
7 package gov.noaa.gdsg.xmldbremote.commandLine;
8
9 import org.apache.commons.cli.Options;
10 import org.apache.commons.cli.CommandLine;
11 import org.apache.commons.cli.Parser;
12 import org.apache.commons.cli.BasicParser;
13 import org.apache.commons.cli.HelpFormatter;
14 import org.xmldb.api.base.Database;
15
16 import gov.noaa.gdsg.xmldbremote.manage.CollectionManager;
17 import gov.noaa.gdsg.xmldbremote.manage.CollectionQuerier;
18 import gov.noaa.gdsg.xmldbremote.manage.RecordInserter;
19 import gov.noaa.gdsg.xmldbremote.manage.RecordRemover;
20
21 import java.io.InputStream;
22 import java.io.FileInputStream;
23 import java.io.File;
24 import java.net.URI;
25 import java.util.Properties;
26
27 import org.w3c.dom.Node;
28
29 import org.apache.log4j.Logger;
30 import org.apache.xml.serializer.DOMSerializer;
31 import org.apache.xml.serializer.Serializer;
32 import org.apache.xml.serializer.SerializerFactory;
33 import org.xmldb.api.base.Resource;
34 import org.xmldb.api.base.ResourceSet;
35 import org.xmldb.api.modules.XMLResource;
36
37 /***
38 * Inserts the specified file into the specified database.
39 *
40 * @version $Id: ResourceManager.java,v 1.1 2004/10/29 17:47:04 mrxtravis Exp $
41 * @author tns
42 */
43 public class ResourceManager {
44
45 private static Logger log = Logger.getLogger(ResourceManager.class);
46
47 /*** Creates a new instance of InsertResource */
48 public ResourceManager() {
49 }
50
51 private static Options options = null;
52
53 static {
54 options = new Options();
55 options.addOption("l","url",true,"Sets the Service URL");
56 options.addOption("d","directory", true, "The directory which contains XML files for Add/Modify");
57 options.addOption("f","file",true,"The XML File used to Add/Modify a resource.");
58 options.addOption("r","uri",true,"A URI of a record to insert.");
59 options.addOption("s","string",true,"The XML String used to Add/Modify a resource.");
60 options.addOption("h","help",false,"Help Options");
61 options.addOption("r","resource-id",true, "The ID of a Resource for identification.");
62 options.addOption("c","collection-uri",true, "The Collection URI to access");
63 options.addOption("u","username", true, "The username to access the collection.");
64 options.addOption("p", "password", true, "the password to access the collection");
65 options.addOption("I","insert", false,"Specifies that record(s) should be inserted.");
66
67 options.addOption("D","delete",false,"Specifies that record(s) should be deleted.");
68 options.addOption("X", "debug",false,"Show debugging output.");
69 options.addOption("q","query",true,"Query the specified collection.");
70 }
71 private static void printUsage(){
72 HelpFormatter hl = new HelpFormatter();
73 hl.printHelp(80,"java -jar <jar>","Header",options,"footer");
74 }
75
76 /***
77 * @param args the command line arguments
78 */
79 public static void main(String[] args) {
80 if (args.length == 0){
81 printUsage();
82 System.exit(0);
83 }
84
85 CommandLine cl = null;
86 try {
87 Parser parser = new BasicParser();
88 cl = parser.parse(options,args);
89
90
91 if (cl.hasOption('h')){
92 printUsage();
93 System.exit(0);
94 }
95
96 if (cl.hasOption('I')){
97 log.info("Starting Inserts");
98 doInsert(cl);
99 log.info("Finished Inserts");
100 }
101 else if (cl.hasOption('D')){
102 doDelete(cl);
103 }
104 else if (cl.hasOption('q')){
105 queryResource(cl);
106 }
107 else {
108 log.info("Please Specify (I)nsert ,(D)elete or (q)uery.");
109 printUsage();
110 System.exit(0);
111 }
112 } catch (Exception e){
113 if (cl == null || cl.hasOption('X')){
114
115 e.printStackTrace();
116 }
117 else {
118 log.info(e.toString());
119 }
120 }
121
122 }
123
124 public static void doDelete(CommandLine cl) throws Exception {
125 String[] resourceIds = cl.getOptionValues('r');
126 if (resourceIds == null || resourceIds.length == 0){
127 throw new NullPointerException("No resource ids were specified");
128 }
129 CollectionManager collectionManager = createCollectionManager(cl);
130 RecordRemover remover = new RecordRemover();
131 remover.setCollectionManager(collectionManager);
132 for (int i = 0; i < resourceIds.length; i++){
133 log.info("Removing resource:" + resourceIds[i]);
134 remover.removeRecord(resourceIds[i]);
135 log.info("...Done.");
136 }
137
138 }
139
140 public static void queryResource(CommandLine cl) throws Exception {
141 String[] uris = cl.getOptionValues('r');
142 String[] queries = cl.getOptionValues('q');
143 CollectionManager collectionManager = createCollectionManager(cl);
144 CollectionQuerier querier = new CollectionQuerier();
145 querier.setCollectionManager(collectionManager);
146 ResourceSet resourceSet = querier.query(queries[0]);
147
148
149 Properties prop = new Properties();
150 prop.setProperty("method", "TEXT");
151 Serializer ser = SerializerFactory.getSerializer(prop);
152 ser.setOutputStream(System.out);
153 DOMSerializer domSer = ser.asDOMSerializer();
154 long size = resourceSet.getSize();
155 for (long i = 0; i < size; i++){
156 Resource resource = resourceSet.getResource(i);
157 if ("XMLResource".equals(resource.getResourceType())){
158 XMLResource xResource = (XMLResource) resource;
159 domSer.serialize(xResource.getContentAsDOM());
160 }
161 System.out.println();
162 }
163
164 }
165
166 public static void doInsert(CommandLine cl) throws Exception {
167
168 String[] files = cl.getOptionValues('f');
169 String[] uris = cl.getOptionValues('r');
170 String[] dirs = cl.getOptionValues('d');
171
172
173 if ( (files == null || files.length == 0) && ( uris == null || uris.length == 0)
174 && (dirs == null || dirs.length ==0 )){
175 log.info("No resources specified for Insert.");
176 printUsage();
177 System.exit(0);
178 }
179
180 CollectionManager collectionManager = createCollectionManager(cl);
181 RecordInserter inserter = new RecordInserter();
182 inserter.setCollectionManager(collectionManager);
183
184
185 if (files != null){
186 for (int i = 0; i < files.length; i++){
187 log.info("Inserting file:" + files[i]);
188 FileInputStream fis = null;
189 try {
190 fis = new FileInputStream(files[i]);
191 Node node = isToNode(fis);
192 inserter.insertRecord(node);
193 log.info("Done");
194 fis.close();
195 } catch (Exception e){
196 log.info("Error processing file:" + files[i] + " " + e.getMessage());
197 log.debug("There error is:",e );
198 } finally {
199 if (fis != null){
200 fis.close();
201 }
202 }
203 }
204 }
205
206
207 if (uris != null){
208 for (int i = 0; i < uris.length; i++){
209 log.info("Inserting URI:" + uris[i]);
210 InputStream is = null;
211 try {
212 URI uri = new URI(uris[i]);
213 is = uri.toURL().openStream();
214 Node node = isToNode(is);
215 inserter.insertRecord(node);
216 } catch (Exception e){
217 log.info("Error processing uri:" + uris[i] + " " + e.getMessage());
218 log.debug("The error was:",e);
219 } finally {
220 if (is != null){
221 is.close();
222 }
223 }
224 }
225 }
226
227
228 if (dirs != null){
229 for (int i = 0; i < dirs.length; i++){
230 try {
231 log.info("Working on directory:" + dirs[i]);
232 File dir = new File(dirs[i]);
233 File[] dirFiles = dir.listFiles(new XmlFileFilter());
234 for (int j = 0; j < dirFiles.length; j++){
235 log.info(" Working in file " + dirFiles[j]);
236 FileInputStream fis = new FileInputStream(dirFiles[j]);
237 Node node = isToNode(fis);
238 inserter.insertRecord(node);
239 fis.close();
240 log.info("...Done.");
241 }
242 } catch (Exception e){
243 log.info("Error processing directory:" + dirs[i] + " " + e.getMessage());
244 log.debug("The error was:",e);
245 }
246
247 }
248 }
249 }
250
251 /***Input Stream To Node */
252 public static Node isToNode(InputStream is) throws Exception {
253 javax.xml.parsers.DocumentBuilderFactory factory =
254 new org.apache.xerces.jaxp.DocumentBuilderFactoryImpl();
255 javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();
256 Node node = builder.parse(is);
257 if (log.isDebugEnabled()){
258 java.io.StringWriter sw = new java.io.StringWriter();
259 org.apache.xalan.processor.TransformerFactoryImpl transFactory =
260 new org.apache.xalan.processor.TransformerFactoryImpl();
261 javax.xml.transform.stream.StreamResult result =
262 new javax.xml.transform.stream.StreamResult(sw);
263
264 javax.xml.transform.dom.DOMSource source =
265 new javax.xml.transform.dom.DOMSource(node);
266
267 transFactory.newTransformer().transform(source,result);
268 log.debug("Parsed node:");
269 log.debug(sw.getBuffer().toString());
270 }
271 return node;
272 }
273
274 /***Create the collection manager with command line args */
275 public static CollectionManager createCollectionManager(CommandLine cl) throws Exception {
276 Database database = getDatabase(cl);
277 String collectionURI = cl.getOptionValue('c');
278 String userName = cl.getOptionValue('u');
279 String password = cl.getOptionValue('p');
280
281 log.info("Setting up Collection Manager");
282 CollectionManager collectionManager = new CollectionManager();
283 collectionManager.setDatabase(database);
284 collectionManager.setCollectionURI(collectionURI);
285 collectionManager.setPassword(password);
286 collectionManager.setUserName(userName);
287 return collectionManager;
288
289 }
290
291
292
293 /***If l is specified use that as an endpoint, otherwise use the default
294 *@param cl The command line.
295 */
296 public static Database getDatabase(CommandLine cl) throws Exception {
297 String dbUrl = cl.getOptionValue('l');
298
299 if (dbUrl == null){
300
301 log.info("Connecting to the database");
302 return gov.noaa.gdsg.xmldbremote.xmldbClient.XmldbClientDatabase.newDefaultDatabase();
303 }
304 else {
305 log.info("Connecting to the database located at " + dbUrl);
306 return gov.noaa.gdsg.xmldbremote.xmldbClient.XmldbClientDatabase.newDatabase(dbUrl);
307 }
308
309 }
310
311
312
313 }
314