KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > packager > Packager


1 /*
2  * Packager.java
3  *
4  * Version: $Revision: 1.4 $
5  *
6  * Date: $Date: 2006/07/03 15:54:43 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40
41 package org.dspace.app.packager;
42
43 import java.io.FileInputStream JavaDoc;
44 import java.io.FileOutputStream JavaDoc;
45 import java.io.InputStream JavaDoc;
46 import java.io.OutputStream JavaDoc;
47
48 import org.apache.commons.cli.CommandLine;
49 import org.apache.commons.cli.CommandLineParser;
50 import org.apache.commons.cli.HelpFormatter;
51 import org.apache.commons.cli.Options;
52 import org.apache.commons.cli.PosixParser;
53 import org.dspace.content.Collection;
54 import org.dspace.content.DSpaceObject;
55 import org.dspace.content.Item;
56 import org.dspace.content.InstallItem;
57 import org.dspace.content.WorkspaceItem;
58 import org.dspace.content.packager.PackageDisseminator;
59 import org.dspace.content.packager.PackageParameters;
60 import org.dspace.content.packager.PackageIngester;
61 import org.dspace.core.Constants;
62 import org.dspace.core.Context;
63 import org.dspace.core.PluginManager;
64 import org.dspace.eperson.EPerson;
65 import org.dspace.handle.HandleManager;
66 import org.dspace.workflow.WorkflowManager;
67 import org.dspace.workflow.WorkflowItem;
68
69 /**
70  * Command-line interface to the Packager plugin.
71  * <p>
72  * This class ONLY exists to provide a CLI for the packager plugins. It does not
73  * "manage" the plugins and it is not called from within DSpace, but the name
74  * follows a DSpace convention.
75  * <p>
76  * It can invoke one of the Submission (SIP) packagers to create a new DSpace
77  * Item out of a package, or a Dissemination (DIP) packager to write an Item out
78  * as a package.
79  * <p>
80  * Usage is as follows:<br>
81  * (Add the -h option to get the command to show its own help)
82  *
83  * <pre>
84  * 1. To submit a SIP:
85  * java org.dspace.app.packager.Packager
86  * -e {ePerson}
87  * -t {PackagerType}
88  * -c {collection-handle} [ -c {collection} ...]
89  * -o {name}={value} [ -o {name}={value} ..]
90  * [-w]
91  * {package-filename}
92  *
93  * {PackagerType} must match one of the aliases of the chosen Packager
94  * plugin.
95  *
96  * The &quot;-w&quot; option circumvents Workflow, and is optional. The &quot;-o&quot;
97  * option, which may be repeated, passes options to the packager
98  * (e.g. &quot;metadataOnly&quot; to a DIP packager).
99  *
100  * 2. To write out a DIP:
101  * java org.dspace.content.packager.Packager
102  * -d
103  * -e {ePerson}
104  * -t {PackagerType}
105  * -i {item-handle}
106  * -o {name}={value} [ -o {name}={value} ..]
107  * {package-filename}
108  *
109  * The &quot;-d&quot; switch chooses a Dissemination packager, and is required.
110  * The &quot;-o&quot; option, which may be repeated, passes options to the packager
111  * (e.g. &quot;metadataOnly&quot; to a DIP packager).
112  * </pre>
113  *
114  * Note that {package-filename} may be "-" for standard input or standard
115  * output, respectively.
116  *
117  * @author Larry Stone
118  * @version $Revision: 1.4 $
119  */

120 public class Packager
121 {
122     // die from illegal command line
123
private static void usageError(String JavaDoc msg)
124     {
125         System.out.println(msg);
126         System.out.println(" (run with -h flag for details)");
127         System.exit(1);
128     }
129
130     public static void main(String JavaDoc[] argv) throws Exception JavaDoc
131     {
132         Options options = new Options();
133         options.addOption("c", "collection", true,
134                 "destination collection(s) Handle (repeatable)");
135         options.addOption("e", "eperson", true,
136                 "email address of eperson doing importing");
137         options
138                 .addOption(
139                         "w",
140                         "install",
141                         false,
142                         "disable workflow; install immediately without going through collection's workflow");
143         options.addOption("t", "type", true, "package type or MIMEtype");
144         options
145                 .addOption("o", "option", true,
146                         "Packager option to pass to plugin, \"name=value\" (repeatable)");
147         options.addOption("d", "disseminate", false,
148                 "Disseminate package (output); default is to submit.");
149         options.addOption("i", "item", true, "Handle of item to disseminate.");
150         options.addOption("h", "help", false, "help");
151
152         CommandLineParser parser = new PosixParser();
153         CommandLine line = parser.parse(options, argv);
154
155         String JavaDoc sourceFile = null;
156         String JavaDoc eperson = null;
157         String JavaDoc[] collections = null;
158         boolean useWorkflow = true;
159         String JavaDoc packageType = null;
160         boolean submit = true;
161         String JavaDoc itemHandle = null;
162         PackageParameters pkgParams = new PackageParameters();
163
164         if (line.hasOption('h'))
165         {
166             HelpFormatter myhelp = new HelpFormatter();
167             myhelp.printHelp("Packager [options] package-file|-\n",
168                     options);
169             System.out.println("\nAvailable Submission Package (SIP) types:");
170             String JavaDoc pn[] = PluginManager
171                     .getAllPluginNames(PackageIngester.class);
172             for (int i = 0; i < pn.length; ++i)
173                 System.out.println(" " + pn[i]);
174             System.out
175                     .println("\nAvailable Dissemination Package (DIP) types:");
176             pn = PluginManager.getAllPluginNames(PackageDisseminator.class);
177             for (int i = 0; i < pn.length; ++i)
178                 System.out.println(" " + pn[i]);
179             System.exit(0);
180         }
181         if (line.hasOption('w'))
182             useWorkflow = false;
183         if (line.hasOption('e'))
184             eperson = line.getOptionValue('e');
185         if (line.hasOption('c'))
186             collections = line.getOptionValues('c');
187         if (line.hasOption('t'))
188             packageType = line.getOptionValue('t');
189         if (line.hasOption('i'))
190             itemHandle = line.getOptionValue('i');
191         String JavaDoc files[] = line.getArgs();
192         if (files.length > 0)
193             sourceFile = files[0];
194         if (line.hasOption('d'))
195             submit = false;
196         if (line.hasOption('o'))
197         {
198             String JavaDoc popt[] = line.getOptionValues('o');
199             for (int i = 0; i < popt.length; ++i)
200             {
201                 String JavaDoc pair[] = popt[i].split("\\=", 2);
202                 if (pair.length == 2)
203                     pkgParams.addProperty(pair[0].trim(), pair[1].trim());
204                 else if (pair.length == 1)
205                     pkgParams.addProperty(pair[0].trim(), "");
206                 else
207                     System.err
208                             .println("Warning: Illegal package option format: \""
209                                     + popt[i] + "\"");
210             }
211         }
212
213         // Sanity checks on arg list: required args
214
if (sourceFile == null || eperson == null || packageType == null
215                 || (submit && collections == null))
216         {
217             System.err
218                     .println("Error - missing a REQUIRED argument or option.\n");
219             HelpFormatter myhelp = new HelpFormatter();
220             myhelp.printHelp("PackageManager [options] package-file|-\n",
221                     options);
222             System.exit(0);
223         }
224
225         // find the EPerson, assign to context
226
Context context = new Context();
227         EPerson myEPerson = null;
228         myEPerson = EPerson.findByEmail(context, eperson);
229         if (myEPerson == null)
230             usageError("Error, eperson cannot be found: " + eperson);
231         context.setCurrentUser(myEPerson);
232
233         if (submit)
234         {
235             // make sure we have an input file
236
InputStream JavaDoc source = (sourceFile.equals("-")) ? System.in
237                     : new FileInputStream JavaDoc(sourceFile);
238
239             PackageIngester sip = (PackageIngester) PluginManager
240                     .getNamedPlugin(PackageIngester.class, packageType);
241             if (sip == null)
242                 usageError("Error, Unknown package type: " + packageType);
243
244             // find collections
245
Collection[] mycollections = null;
246
247             System.out.println("Destination collections:");
248
249             // validate each collection arg to see if it's a real collection
250
mycollections = new Collection[collections.length];
251             for (int i = 0; i < collections.length; i++)
252             {
253                 // sanity check: did handle resolve, and to a collection?
254
DSpaceObject dso = HandleManager.resolveToObject(context,
255                         collections[i]);
256                 if (dso == null)
257                     throw new IllegalArgumentException JavaDoc(
258                             "Bad collection list -- "
259                                     + "Cannot resolve collection handle \""
260                                     + collections[i] + "\"");
261                 else if (dso.getType() != Constants.COLLECTION)
262                     throw new IllegalArgumentException JavaDoc(
263                             "Bad collection list -- " + "Object at handle \""
264                                     + collections[i]
265                                     + "\" is not a collection!");
266                 mycollections[i] = (Collection) dso;
267                 System.out.println((i == 0 ? " Owning " : " ")
268                         + " Collection: "
269                         + mycollections[i].getMetadata("name"));
270             }
271
272             try
273             {
274                 WorkspaceItem wi = sip.ingest(context, mycollections[0],
275                         source, pkgParams, null);
276                 if (useWorkflow)
277                 {
278                     String JavaDoc handle = null;
279
280                     // Check if workflow completes immediately, and
281
// return Handle if so.
282
WorkflowItem wfi = WorkflowManager.startWithoutNotify(context, wi);
283
284                     if (wfi.getState() == WorkflowManager.WFSTATE_ARCHIVE)
285                     {
286                         Item ni = wfi.getItem();
287                         handle = HandleManager.findHandle(context, ni);
288                     }
289                     if (handle == null)
290                     System.out.println("Created Workflow item, ID="
291                                 + String.valueOf(wfi.getID()));
292                     else
293                         System.out.println("Created and installed item, handle="+handle);
294                 }
295                 else
296                 {
297                     InstallItem.installItem(context, wi);
298                     System.out.println("Created and installed item, handle="
299                             + HandleManager.findHandle(context, wi.getItem()));
300                 }
301                 context.complete();
302                 System.exit(0);
303             }
304             catch (Exception JavaDoc e)
305             {
306                 // abort all operations
307
context.abort();
308                 e.printStackTrace();
309                 System.out.println(e);
310                 System.exit(1);
311             }
312         }
313         else
314         {
315             OutputStream JavaDoc dest = (sourceFile.equals("-")) ? (OutputStream JavaDoc) System.out
316                     : (OutputStream JavaDoc) (new FileOutputStream JavaDoc(sourceFile));
317
318             PackageDisseminator dip = (PackageDisseminator) PluginManager
319                     .getNamedPlugin(PackageDisseminator.class, packageType);
320             if (dip == null)
321                 usageError("Error, Unknown package type: " + packageType);
322
323             DSpaceObject dso = HandleManager.resolveToObject(context,
324                     itemHandle);
325             if (dso == null)
326                 throw new IllegalArgumentException JavaDoc("Bad Item handle -- "
327                         + "Cannot resolve handle \"" + itemHandle);
328             dip.disseminate(context, dso, pkgParams, dest);
329         }
330     }
331 }
332
Popular Tags