KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > tools > Import


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.tools;
18
19 import java.io.File JavaDoc;
20 import java.io.Serializable JavaDoc;
21 import java.nio.charset.Charset JavaDoc;
22
23 import org.alfresco.repo.importer.ACPImportPackageHandler;
24 import org.alfresco.repo.importer.FileImportPackageHandler;
25 import org.alfresco.service.cmr.repository.NodeRef;
26 import org.alfresco.service.cmr.repository.StoreRef;
27 import org.alfresco.service.cmr.security.AccessPermission;
28 import org.alfresco.service.cmr.view.ImportPackageHandler;
29 import org.alfresco.service.cmr.view.ImporterBinding;
30 import org.alfresco.service.cmr.view.ImporterException;
31 import org.alfresco.service.cmr.view.ImporterProgress;
32 import org.alfresco.service.cmr.view.ImporterService;
33 import org.alfresco.service.cmr.view.Location;
34 import org.alfresco.service.cmr.view.ImporterBinding.UUID_BINDING;
35 import org.alfresco.service.namespace.QName;
36
37
38
39 /**
40  * Import Tool.
41  *
42  * @author David Caruana
43  */

44 public class Import extends Tool
45 {
46     /** Import Tool Context */
47     private ImportContext context;
48     
49     
50     /**
51      * Entry Point
52      *
53      * @param args
54      */

55     public static void main(String JavaDoc[] args)
56     {
57         Tool tool = new Import();
58         tool.start(args);
59     }
60     
61     /* (non-Javadoc)
62      * @see org.alfresco.tools.Tool#processArgs(java.lang.String[])
63      */

64     @Override JavaDoc
65     /*package*/ ToolContext processArgs(String JavaDoc[] args)
66     {
67         context = new ImportContext();
68         context.setLogin(true);
69
70         int i = 0;
71         while (i < args.length)
72         {
73             if (args[i].equals("-h") || args[i].equals("-help"))
74             {
75                 context.setHelp(true);
76                 break;
77             }
78             else if (args[i].equals("-s") || args[i].equals("-store"))
79             {
80                 i++;
81                 if (i == args.length || args[i].length() == 0)
82                 {
83                     throw new ToolException("The value <store> for the option -store must be specified");
84                 }
85                 context.storeRef = new StoreRef(args[i]);
86             }
87             else if (args[i].equals("-p") || args[i].equals("-path"))
88             {
89                 i++;
90                 if (i == args.length || args[i].length() == 0)
91                 {
92                     throw new ToolException("The value <path> for the option -path must be specified");
93                 }
94                 context.path = args[i];
95             }
96             else if (args[i].equals("-d") || args[i].equals("-dir"))
97             {
98                 i++;
99                 if (i == args.length || args[i].length() == 0)
100                 {
101                     throw new ToolException("The value <dir> for the option -dir must be specified");
102                 }
103                 context.sourceDir = args[i];
104             }
105             else if (args[i].equals("-user"))
106             {
107                 i++;
108                 if (i == args.length || args[i].length() == 0)
109                 {
110                     throw new ToolException("The value <user> for the option -user must be specified");
111                 }
112                 context.setUsername(args[i]);
113             }
114             else if (args[i].equals("-pwd"))
115             {
116                 i++;
117                 if (i == args.length || args[i].length() == 0)
118                 {
119                     throw new ToolException("The value <password> for the option -pwd must be specified");
120                 }
121                 context.setPassword(args[i]);
122             }
123             else if (args[i].equals("-encoding"))
124             {
125                 i++;
126                 if (i == args.length || args[i].length() == 0)
127                 {
128                     throw new ToolException("The value <encoding> for the option -encoding must be specified");
129                 }
130                 context.encoding = args[i];
131             }
132             else if (args[i].equals("-uuidBinding"))
133             {
134                 i++;
135                 try
136                 {
137                     context.uuidBinding = UUID_BINDING.valueOf(UUID_BINDING.class, args[i]);
138                 }
139                 catch(IllegalArgumentException JavaDoc e)
140                 {
141                     throw new ToolException("The value " + args[i] + " is an invalid uuidBinding");
142                 }
143             }
144             else if (args[i].equals("-quiet"))
145             {
146                 context.setQuiet(true);
147             }
148             else if (args[i].equals("-verbose"))
149             {
150                 context.setVerbose(true);
151             }
152             else if (i == (args.length - 1))
153             {
154                 context.packageName = args[i];
155             }
156             else
157             {
158                 throw new ToolException("Unknown option " + args[i]);
159             }
160
161             // next argument
162
i++;
163         }
164
165         return context;
166     }
167     
168     /* (non-Javadoc)
169      * @see org.alfresco.tools.Tool#displayHelp()
170      */

171     @Override JavaDoc
172     /*package*/ void displayHelp()
173     {
174         System.out.println("Usage: import -user username -s[tore] store [options] packagename");
175         System.out.println("");
176         System.out.println("username: username for login");
177         System.out.println("store: the store to import into the form of scheme://store_name");
178         System.out.println("packagename: the filename to import from (with or without extension)");
179         System.out.println("");
180         System.out.println("Options:");
181         System.out.println(" -h[elp] display this help");
182         System.out.println(" -p[ath] the path within the store to extract into (default: /)");
183         System.out.println(" -d[ir] the source directory to import from (default: current directory)");
184         System.out.println(" -pwd password for login");
185         System.out.println(" -encoding package file encoding (default: " + Charset.defaultCharset() + ")");
186         System.out.println(" -uuidBinding CREATE_NEW, REMOVE_EXISTING, REPLACE_EXISTING, UPDATE_EXISTING, THROW_ON_COLLISION (default: CREATE_NEW)");
187         System.out.println(" -quiet do not display any messages during import");
188         System.out.println(" -verbose report import progress");
189     }
190     
191     /* (non-Javadoc)
192      * @see org.alfresco.tools.Tool#getToolName()
193      */

194     @Override JavaDoc
195     /*package*/ String JavaDoc getToolName()
196     {
197         return "Alfresco Repository Importer";
198     }
199     
200     /* (non-Javadoc)
201      * @see org.alfresco.tools.Tool#execute()
202      */

203     @Override JavaDoc
204     /*package*/ void execute() throws ToolException
205     {
206         ImporterService importer = getServiceRegistry().getImporterService();
207         
208         // determine type of import (from zip or file system)
209
ImportPackageHandler importHandler;
210         if (context.zipFile)
211         {
212             importHandler = new ZipHandler(context.getSourceDir(), context.getPackageFile(), context.encoding);
213         }
214         else
215         {
216             importHandler = new FileHandler(context.getSourceDir(), context.getPackageFile(), context.encoding);
217         }
218         
219         try
220         {
221             ImportBinding binding = new ImportBinding(context.uuidBinding);
222             importer.importView(importHandler, context.getLocation(), binding, new ImportProgress());
223         }
224         catch(ImporterException e)
225         {
226             throw new ToolException("Failed to import package due to " + e.getMessage(), e);
227         }
228     }
229
230     /**
231      * Handler for importing Repository content from zip package
232      *
233      * @author David Caruana
234      */

235     private class ZipHandler extends ACPImportPackageHandler
236     {
237         /**
238          * Construct
239          *
240          * @param sourceDir
241          * @param dataFile
242          * @param dataFileEncoding
243          */

244         public ZipHandler(File JavaDoc sourceDir, File JavaDoc dataFile, String JavaDoc dataFileEncoding)
245         {
246             super(new File JavaDoc(sourceDir, dataFile.getPath()), dataFileEncoding);
247         }
248
249         /**
250          * Log Export Message
251          *
252          * @param message message to log
253          */

254         protected void log(String JavaDoc message)
255         {
256             Import.this.log(message);
257         }
258     }
259     
260     /**
261      * Handler for importing Repository content from file system files
262      *
263      * @author David Caruana
264      */

265     private class FileHandler extends FileImportPackageHandler
266     {
267         /**
268          * Construct
269          *
270          * @param sourceDir
271          * @param dataFile
272          * @param dataFileEncoding
273          */

274         public FileHandler(File JavaDoc sourceDir, File JavaDoc dataFile, String JavaDoc dataFileEncoding)
275         {
276             super(sourceDir, dataFile, dataFileEncoding);
277         }
278
279         /**
280          * Log Export Message
281          *
282          * @param message message to log
283          */

284         protected void log(String JavaDoc message)
285         {
286             Import.this.log(message);
287         }
288     }
289     
290     /**
291      * Report Import Progress
292      *
293      * @author David Caruana
294      */

295     private class ImportProgress implements ImporterProgress
296     {
297         /* (non-Javadoc)
298          * @see org.alfresco.service.cmr.view.ImporterProgress#nodeCreated(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, org.alfresco.service.namespace.QName)
299          */

300         public void nodeCreated(NodeRef nodeRef, NodeRef parentRef, QName assocName, QName childName)
301         {
302             logVerbose("Imported node " + nodeRef + " (parent=" + parentRef + ", childname=" + childName + ", association=" + assocName + ")");
303         }
304
305         /*
306          * (non-Javadoc)
307          * @see org.alfresco.service.cmr.view.ImporterProgress#nodeLinked(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, org.alfresco.service.namespace.QName)
308          */

309         public void nodeLinked(NodeRef nodeRef, NodeRef parentRef, QName assocName, QName childName)
310         {
311         }
312         
313         /* (non-Javadoc)
314          * @see org.alfresco.service.cmr.view.ImporterProgress#contentCreated(org.alfresco.service.cmr.repository.NodeRef, java.lang.String)
315          */

316         public void contentCreated(NodeRef nodeRef, String JavaDoc sourceUrl)
317         {
318         }
319
320         /* (non-Javadoc)
321          * @see org.alfresco.service.cmr.view.ImporterProgress#propertySet(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, java.io.Serializable)
322          */

323         public void propertySet(NodeRef nodeRef, QName property, Serializable JavaDoc value)
324         {
325         }
326
327         /* (non-Javadoc)
328          * @see org.alfresco.service.cmr.view.ImporterProgress#permissionSet(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.cmr.security.AccessPermission)
329          */

330         public void permissionSet(NodeRef nodeRef, AccessPermission permission)
331         {
332         }
333
334         /* (non-Javadoc)
335          * @see org.alfresco.service.cmr.view.ImporterProgress#aspectAdded(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName)
336          */

337         public void aspectAdded(NodeRef nodeRef, QName aspect)
338         {
339         }
340
341         /* (non-Javadoc)
342          * @see org.alfresco.service.cmr.view.ImporterProgress#started()
343          */

344         public void started()
345         {
346         }
347
348         /* (non-Javadoc)
349          * @see org.alfresco.service.cmr.view.ImporterProgress#completed()
350          */

351         public void completed()
352         {
353         }
354
355         /* (non-Javadoc)
356          * @see org.alfresco.service.cmr.view.ImporterProgress#error(java.lang.Throwable)
357          */

358         public void error(Throwable JavaDoc e)
359         {
360         }
361     }
362     
363     /**
364      * Import Tool Context
365      *
366      * @author David Caruana
367      */

368     private class ImportContext extends ToolContext
369     {
370         /** Store Reference to import into */
371         private StoreRef storeRef;
372         /** Path to import into */
373         private String JavaDoc path;
374         /** Source directory to import from */
375         private String JavaDoc sourceDir;
376         /** The package name to import */
377         private String JavaDoc packageName;
378         /** The package encoding */
379         private String JavaDoc encoding = null;
380         /** The UUID Binding */
381         private UUID_BINDING uuidBinding = UUID_BINDING.CREATE_NEW;
382         /** Zip Package? */
383         private boolean zipFile = false;
384
385         /* (non-Javadoc)
386          * @see org.alfresco.tools.ToolContext#validate()
387          */

388         @Override JavaDoc
389         /*package*/ void validate()
390         {
391             super.validate();
392             
393             if (storeRef == null)
394             {
395                 throw new ToolException("Store to import into has not been specified.");
396             }
397             if (packageName == null)
398             {
399                 throw new ToolException("Package name has not been specified.");
400             }
401             if (sourceDir != null)
402             {
403                 File JavaDoc fileSourceDir = getSourceDir();
404                 if (fileSourceDir.exists() == false)
405                 {
406                     throw new ToolException("Source directory " + fileSourceDir.getAbsolutePath() + " does not exist.");
407                 }
408             }
409             if (packageName.endsWith(".acp"))
410             {
411                 File JavaDoc packageFile = new File JavaDoc(getSourceDir(), packageName);
412                 if (!packageFile.exists())
413                 {
414                     throw new ToolException("Package zip file " + packageFile.getAbsolutePath() + " does not exist.");
415                 }
416                 zipFile = true;
417             }
418             else
419             {
420                 File JavaDoc packageFile = new File JavaDoc(getSourceDir(), getDataFile().getPath());
421                 if (!packageFile.exists())
422                 {
423                     throw new ToolException("Package file " + packageFile.getAbsolutePath() + " does not exist.");
424                 }
425             }
426         }
427
428         /**
429          * Get the location within the Repository to import into
430          *
431          * @return the location
432          */

433         private Location getLocation()
434         {
435             Location location = new Location(storeRef);
436             location.setPath(path);
437             return location;
438         }
439         
440         /**
441          * Get the source directory
442          *
443          * @return the source directory (or null if current directory)
444          */

445         private File JavaDoc getSourceDir()
446         {
447             File JavaDoc dir = (sourceDir == null) ? null : new File JavaDoc(sourceDir);
448             return dir;
449         }
450
451         /**
452          * Get the xml import file
453          *
454          * @return the package file
455          */

456         private File JavaDoc getDataFile()
457         {
458             String JavaDoc dataFile = (packageName.indexOf('.') != -1) ? packageName : packageName + ".xml";
459             File JavaDoc file = new File JavaDoc(dataFile);
460             return file;
461         }
462         
463         /**
464          * Get the zip import file (.acp - alfresco content package)
465          *
466          * @return the zip package file
467          */

468         private File JavaDoc getPackageFile()
469         {
470             return (zipFile) ? new File JavaDoc(packageName) : getDataFile();
471         }
472     }
473  
474
475     /**
476      * Import Tool Binding
477      *
478      * @author davidc
479      */

480     private class ImportBinding implements ImporterBinding
481     {
482         private UUID_BINDING uuidBinding = null;
483     
484         /**
485          * Construct
486          *
487          * @param uuidBinding
488          */

489         public ImportBinding(UUID_BINDING uuidBinding)
490         {
491             this.uuidBinding = uuidBinding;
492         }
493         
494         /*
495          * (non-Javadoc)
496          * @see org.alfresco.service.cmr.view.ImporterBinding#getUUIDBinding()
497          */

498         public UUID_BINDING getUUIDBinding()
499         {
500             return uuidBinding;
501         }
502
503         /*
504          * (non-Javadoc)
505          * @see org.alfresco.service.cmr.view.ImporterBinding#allowReferenceWithinTransaction()
506          */

507         public boolean allowReferenceWithinTransaction()
508         {
509             return false;
510         }
511
512         /*
513          * (non-Javadoc)
514          * @see org.alfresco.service.cmr.view.ImporterBinding#getValue(java.lang.String)
515          */

516         public String JavaDoc getValue(String JavaDoc key)
517         {
518             return null;
519         }
520     }
521         
522 }
523
Popular Tags