| 1 17 package org.alfresco.tools; 18 19 import java.io.File ; 20 import java.io.Serializable ; 21 import java.nio.charset.Charset ; 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 44 public class Import extends Tool 45 { 46 47 private ImportContext context; 48 49 50 55 public static void main(String [] args) 56 { 57 Tool tool = new Import(); 58 tool.start(args); 59 } 60 61 64 @Override  65 ToolContext processArgs(String [] 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 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 i++; 163 } 164 165 return context; 166 } 167 168 171 @Override  172 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 194 @Override  195 String getToolName() 196 { 197 return "Alfresco Repository Importer"; 198 } 199 200 203 @Override  204 void execute() throws ToolException 205 { 206 ImporterService importer = getServiceRegistry().getImporterService(); 207 208 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 235 private class ZipHandler extends ACPImportPackageHandler 236 { 237 244 public ZipHandler(File sourceDir, File dataFile, String dataFileEncoding) 245 { 246 super(new File (sourceDir, dataFile.getPath()), dataFileEncoding); 247 } 248 249 254 protected void log(String message) 255 { 256 Import.this.log(message); 257 } 258 } 259 260 265 private class FileHandler extends FileImportPackageHandler 266 { 267 274 public FileHandler(File sourceDir, File dataFile, String dataFileEncoding) 275 { 276 super(sourceDir, dataFile, dataFileEncoding); 277 } 278 279 284 protected void log(String message) 285 { 286 Import.this.log(message); 287 } 288 } 289 290 295 private class ImportProgress implements ImporterProgress 296 { 297 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 309 public void nodeLinked(NodeRef nodeRef, NodeRef parentRef, QName assocName, QName childName) 310 { 311 } 312 313 316 public void contentCreated(NodeRef nodeRef, String sourceUrl) 317 { 318 } 319 320 323 public void propertySet(NodeRef nodeRef, QName property, Serializable value) 324 { 325 } 326 327 330 public void permissionSet(NodeRef nodeRef, AccessPermission permission) 331 { 332 } 333 334 337 public void aspectAdded(NodeRef nodeRef, QName aspect) 338 { 339 } 340 341 344 public void started() 345 { 346 } 347 348 351 public void completed() 352 { 353 } 354 355 358 public void error(Throwable e) 359 { 360 } 361 } 362 363 368 private class ImportContext extends ToolContext 369 { 370 371 private StoreRef storeRef; 372 373 private String path; 374 375 private String sourceDir; 376 377 private String packageName; 378 379 private String encoding = null; 380 381 private UUID_BINDING uuidBinding = UUID_BINDING.CREATE_NEW; 382 383 private boolean zipFile = false; 384 385 388 @Override  389 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 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 packageFile = new File (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 packageFile = new File (getSourceDir(), getDataFile().getPath()); 421 if (!packageFile.exists()) 422 { 423 throw new ToolException("Package file " + packageFile.getAbsolutePath() + " does not exist."); 424 } 425 } 426 } 427 428 433 private Location getLocation() 434 { 435 Location location = new Location(storeRef); 436 location.setPath(path); 437 return location; 438 } 439 440 445 private File getSourceDir() 446 { 447 File dir = (sourceDir == null) ? null : new File (sourceDir); 448 return dir; 449 } 450 451 456 private File getDataFile() 457 { 458 String dataFile = (packageName.indexOf('.') != -1) ? packageName : packageName + ".xml"; 459 File file = new File (dataFile); 460 return file; 461 } 462 463 468 private File getPackageFile() 469 { 470 return (zipFile) ? new File (packageName) : getDataFile(); 471 } 472 } 473 474 475 480 private class ImportBinding implements ImporterBinding 481 { 482 private UUID_BINDING uuidBinding = null; 483 484 489 public ImportBinding(UUID_BINDING uuidBinding) 490 { 491 this.uuidBinding = uuidBinding; 492 } 493 494 498 public UUID_BINDING getUUIDBinding() 499 { 500 return uuidBinding; 501 } 502 503 507 public boolean allowReferenceWithinTransaction() 508 { 509 return false; 510 } 511 512 516 public String getValue(String key) 517 { 518 return null; 519 } 520 } 521 522 } 523 | Popular Tags |