001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU General
007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
008: * License("CDDL") (collectively, the "License"). You may not use this file except in
009: * compliance with the License. You can obtain a copy of the License at
010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
011: * License for the specific language governing permissions and limitations under the
012: * License. When distributing the software, include this License Header Notice in
013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
014: * designates this particular file as subject to the "Classpath" exception as
015: * provided by Sun in the GPL Version 2 section of the License file that
016: * accompanied this code. If applicable, add the following below the License Header,
017: * with the fields enclosed by brackets [] replaced by your own identifying
018: * information: "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Contributor(s):
021: *
022: * The Original Software is NetBeans. The Initial Developer of the Original Software
023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
024: * Rights Reserved.
025: *
026: * If you wish your version of this file to be governed by only the CDDL or only the
027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
029: * you do not indicate a single choice of license, a recipient has the option to
030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
031: * to extend the choice of license to its licensees as provided above. However, if
032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
033: * the option applies only if the new code is made subject to such option by the
034: * copyright holder.
035: */
036:
037: package org.netbeans.installer.infra.server.ejb;
038:
039: import java.io.BufferedReader;
040: import java.io.File;
041: import java.io.FileInputStream;
042: import java.io.FileOutputStream;
043: import java.io.IOException;
044: import java.io.InputStreamReader;
045: import java.io.OutputStreamWriter;
046: import java.io.PrintWriter;
047: import java.io.UnsupportedEncodingException;
048: import java.net.URLEncoder;
049: import java.util.ArrayList;
050: import java.util.HashMap;
051: import java.util.LinkedList;
052: import java.util.List;
053: import java.util.Locale;
054: import java.util.Map;
055: import java.util.Queue;
056: import java.util.concurrent.locks.ReentrantLock;
057: import java.util.jar.JarOutputStream;
058: import java.util.zip.ZipEntry;
059: import javax.ejb.Stateless;
060: import org.netbeans.installer.Installer;
061: import org.netbeans.installer.downloader.DownloadManager;
062: import org.netbeans.installer.product.components.Product;
063: import org.netbeans.installer.product.components.Group;
064: import org.netbeans.installer.product.Registry;
065: import org.netbeans.installer.product.RegistryNode;
066: import org.netbeans.installer.product.filters.TrueFilter;
067: import org.netbeans.installer.utils.FileUtils;
068: import org.netbeans.installer.utils.LogManager;
069: import org.netbeans.installer.utils.StreamUtils;
070: import org.netbeans.installer.utils.StringUtils;
071: import org.netbeans.installer.utils.SystemUtils;
072: import org.netbeans.installer.utils.XMLUtils;
073: import org.netbeans.installer.utils.applications.JavaUtils;
074: import org.netbeans.installer.utils.exceptions.FinalizationException;
075: import org.netbeans.installer.utils.exceptions.InitializationException;
076: import org.netbeans.installer.utils.exceptions.ParseException;
077: import org.netbeans.installer.utils.exceptions.XMLException;
078: import org.netbeans.installer.utils.helper.ExecutionResults;
079: import org.netbeans.installer.utils.helper.Platform;
080: import org.netbeans.installer.utils.helper.Status;
081: import org.netbeans.installer.utils.helper.Version;
082: import org.netbeans.installer.utils.progress.Progress;
083: import org.w3c.dom.Document;
084:
085: /**
086: *
087: * @author Kirill Sorokin
088: */
089: @Stateless
090: public class ManagerBean implements Manager {
091: /////////////////////////////////////////////////////////////////////////////////
092: // Static
093: private static ReentrantLock bundlesLock = new ReentrantLock();
094:
095: private static Map<String, File> registries = new HashMap<String, File>();
096: private static Map<String, File> bundles = new HashMap<String, File>();
097:
098: /////////////////////////////////////////////////////////////////////////////////
099: // Instance
100: public ManagerBean() {
101: try {
102: ROOT.mkdirs();
103:
104: TEMP.mkdirs();
105: REGISTRIES.mkdirs();
106: UPLOADS.mkdirs();
107: BUNDLES.mkdirs();
108: NBI.mkdirs();
109:
110: if (!REGISTRIES_LIST.exists()) {
111: REGISTRIES_LIST.createNewFile();
112: }
113:
114: loadRegistriesList();
115:
116: Locale.setDefault(new Locale("en", "US"));
117:
118: DownloadManager.getInstance().setLocalDirectory(NBI);
119: DownloadManager.getInstance().setFinishHandler(
120: new DummyFinishHandler());
121:
122: System.setProperty(Installer.LOCAL_DIRECTORY_PATH_PROPERTY,
123: NBI.getAbsolutePath());
124: System.setProperty(Installer.IGNORE_LOCK_FILE_PROPERTY,
125: "true");
126: System.setProperty(LogManager.LOG_TO_CONSOLE_PROPERTY,
127: "false");
128: System.setProperty(Registry.LAZY_LOAD_ICONS_PROPERTY,
129: "true");
130: } catch (IOException e) {
131: e.printStackTrace();
132: } catch (ManagerException e) {
133: e.printStackTrace();
134: }
135: }
136:
137: // registry operations //////////////////////////////////////////////////////////
138: public void addRegistry(String registryName)
139: throws ManagerException {
140: if (registries.get(registryName) == null) {
141: registries.put(registryName,
142: initializeRegistry(registryName));
143: }
144:
145: saveRegistriesList();
146: }
147:
148: public void removeRegistry(String registryName)
149: throws ManagerException {
150: try {
151: if (registries.get(registryName) != null) {
152: FileUtils
153: .deleteFile(registries.get(registryName), true);
154: registries.remove(registryName);
155: }
156: } catch (IOException e) {
157: e.printStackTrace();
158: throw new ManagerException("Could not load registry", e);
159: }
160:
161: saveRegistriesList();
162: }
163:
164: public String getRegistry(String registryName)
165: throws ManagerException {
166: if (registries.get(registryName) == null) {
167: addRegistry(registryName);
168: }
169:
170: final File registryDir = registries.get(registryName);
171: final File registryXml = new File(registryDir, REGISTRY_XML);
172:
173: try {
174: return FileUtils.readFile(registryXml);
175: } catch (IOException e) {
176: e.printStackTrace();
177: throw new ManagerException("Could not load registry", e);
178: }
179: }
180:
181: public List<String> getRegistries() throws ManagerException {
182: return new ArrayList<String>(registries.keySet());
183: }
184:
185: // engine operations ////////////////////////////////////////////////////////////
186: public File getEngine() throws ManagerException {
187: return ENGINE;
188: }
189:
190: public void updateEngine(File engine) throws ManagerException {
191: deleteBundles();
192:
193: ENGINE.delete();
194:
195: try {
196: FileUtils.moveFile(engine, ENGINE);
197: } catch (IOException e) {
198: e.printStackTrace();
199: throw new ManagerException("Could not load registry", e);
200: }
201: }
202:
203: // component operations /////////////////////////////////////////////////////////
204: public void addPackage(String registryName, File archive,
205: String parentUid, String parentVersion,
206: String parentPlatforms, String uriPrefix)
207: throws ManagerException {
208: deleteBundles();
209:
210: if (registries.get(registryName) == null) {
211: addRegistry(registryName);
212: }
213:
214: try {
215: final File localRegistryDir = registries.get(registryName);
216: final File localRegistryXml = new File(localRegistryDir,
217: REGISTRY_XML);
218: final File packageRegistryDir = FileUtils.createTempFile(
219: TEMP, false);
220: final File packageRegistryXml = new File(
221: packageRegistryDir, "registry.xml");
222:
223: FileUtils.unjar(archive, packageRegistryDir);
224: FileUtils.modifyFile(packageRegistryXml,
225: "(\\>)resource:(.*?\\<\\/)", "$1"
226: + uriPrefix.replace("&", "&") + "$2",
227: true);
228:
229: final Registry localRegistry = new Registry();
230: localRegistry.setLocalDirectory(NBI);
231: localRegistry.setFinishHandler(new DummyFinishHandler());
232: localRegistry.loadProductRegistry(localRegistryXml);
233:
234: final Registry packageRegistry = new Registry();
235: packageRegistry.setLocalDirectory(NBI);
236: packageRegistry.setFinishHandler(new DummyFinishHandler());
237: packageRegistry.loadProductRegistry(packageRegistryXml);
238:
239: final Queue<RegistryNode> nodes = new LinkedList<RegistryNode>();
240:
241: for (Product product : packageRegistry.getProducts()) {
242: final List<Product> existingProducts = localRegistry
243: .getProducts(product.getUid(), product
244: .getVersion(), product.getPlatforms());
245:
246: if (existingProducts.size() > 0) {
247: for (Product existingProduct : existingProducts) {
248: nodes.offer(existingProduct);
249: }
250: }
251: }
252:
253: for (Group group : packageRegistry.getGroups()) {
254: if (!group.equals(packageRegistry.getRegistryRoot())) {
255: final Group existingGroup = localRegistry
256: .getGroup(group.getUid());
257:
258: if (existingGroup != null) {
259: nodes.offer(existingGroup);
260: }
261: }
262: }
263:
264: if (nodes.size() > 0) {
265: while (nodes.peek() != null) {
266: final RegistryNode node = nodes.poll();
267:
268: node.getParent().removeChild(node);
269:
270: if (node instanceof Product) {
271: final Product temp = (Product) node;
272: final String path = PRODUCTS
273: + "/"
274: + temp.getUid()
275: + "/"
276: + temp.getVersion()
277: + "/"
278: + StringUtils.asString(temp
279: .getPlatforms(), " ");
280:
281: FileUtils.deleteFile(new File(localRegistryDir,
282: path), true);
283: }
284:
285: if (node instanceof Group) {
286: final Group temp = (Group) node;
287: final String path = GROUPS + "/"
288: + temp.getUid();
289:
290: FileUtils.deleteFile(new File(localRegistryDir,
291: path), true);
292: }
293:
294: for (RegistryNode child : node.getChildren()) {
295: nodes.offer(child);
296: }
297: }
298: }
299:
300: FileUtils.copyFile(new File(packageRegistryDir, PRODUCTS),
301: new File(localRegistryDir, PRODUCTS), true);
302: FileUtils.copyFile(new File(packageRegistryDir, GROUPS),
303: new File(localRegistryDir, GROUPS), true);
304:
305: RegistryNode parent;
306:
307: List<Product> parents = null;
308: if ((parentVersion != null)
309: && !parentVersion.equals("null")
310: && (parentPlatforms != null)
311: && !parentPlatforms.equals("null")) {
312: parents = localRegistry.getProducts(parentUid, Version
313: .getVersion(parentVersion), StringUtils
314: .parsePlatforms(parentPlatforms));
315: }
316: if ((parents == null) || (parents.size() == 0)) {
317: parent = localRegistry.getGroup(parentUid);
318: if (parent == null) {
319: parent = localRegistry.getRegistryRoot();
320: }
321: } else {
322: parent = parents.get(0);
323: }
324:
325: parent.attachRegistry(packageRegistry);
326:
327: localRegistry.saveProductRegistry(localRegistryXml,
328: TrueFilter.INSTANCE, true, true, true);
329:
330: FileUtils.deleteFile(archive);
331: FileUtils.deleteFile(packageRegistryDir, true);
332: } catch (InitializationException e) {
333: e.printStackTrace();
334: throw new ManagerException("Could not update component", e);
335: } catch (XMLException e) {
336: e.printStackTrace();
337: throw new ManagerException("Could not update component", e);
338: } catch (FinalizationException e) {
339: e.printStackTrace();
340: throw new ManagerException("Could not update component", e);
341: } catch (ParseException e) {
342: e.printStackTrace();
343: throw new ManagerException("Could not update component", e);
344: } catch (IOException e) {
345: e.printStackTrace();
346: throw new ManagerException("Could not load registry", e);
347: }
348: }
349:
350: public void removeProduct(String registryName, String uid,
351: String version, String platforms) throws ManagerException {
352: deleteBundles();
353:
354: if (registries.get(registryName) == null) {
355: addRegistry(registryName);
356: }
357:
358: final File registryDir = registries.get(registryName);
359: final File productsDir = new File(registryDir, PRODUCTS);
360: final File groupsDir = new File(registryDir, GROUPS);
361: final File registryXml = new File(registryDir, REGISTRY_XML);
362:
363: try {
364: final Registry registry = new Registry();
365:
366: registry.setLocalDirectory(NBI);
367: registry.setFinishHandler(new DummyFinishHandler());
368: registry.loadProductRegistry(registryXml);
369:
370: final List<Product> existing = registry.getProducts(uid,
371: Version.getVersion(version), StringUtils
372: .parsePlatforms(platforms));
373:
374: if (existing != null) {
375: existing.get(0).getParent()
376: .removeChild(existing.get(0));
377:
378: Queue<RegistryNode> nodes = new LinkedList<RegistryNode>();
379: nodes.offer(existing.get(0));
380:
381: while (nodes.peek() != null) {
382: RegistryNode node = nodes.poll();
383:
384: if (node instanceof Product) {
385: Product temp = (Product) node;
386: FileUtils.deleteFile(new File(productsDir, temp
387: .getUid()
388: + "/" + temp.getVersion()));
389: }
390:
391: if (node instanceof Group) {
392: Group temp = (Group) node;
393: FileUtils.deleteFile(new File(groupsDir, temp
394: .getUid()));
395: }
396:
397: for (RegistryNode child : node.getChildren()) {
398: nodes.offer(child);
399: }
400: }
401: }
402:
403: registry.saveProductRegistry(registryXml,
404: TrueFilter.INSTANCE, true, true, true);
405: } catch (InitializationException e) {
406: e.printStackTrace();
407: throw new ManagerException("Could not remove component", e);
408: } catch (FinalizationException e) {
409: e.printStackTrace();
410: throw new ManagerException("Could not remove component", e);
411: } catch (ParseException e) {
412: e.printStackTrace();
413: throw new ManagerException("Could not update component", e);
414: } catch (IOException e) {
415: e.printStackTrace();
416: throw new ManagerException("Could not load registry", e);
417: }
418: }
419:
420: public void removeGroup(String registryName, String uid)
421: throws ManagerException {
422: deleteBundles();
423:
424: if (registries.get(registryName) == null) {
425: addRegistry(registryName);
426: }
427:
428: final File registryDir = registries.get(registryName);
429: final File productsDir = new File(registryDir, PRODUCTS);
430: final File groupsDir = new File(registryDir, GROUPS);
431: final File registryXml = new File(registryDir, REGISTRY_XML);
432:
433: try {
434: final Registry registry = new Registry();
435:
436: registry.setLocalDirectory(NBI);
437: registry.setFinishHandler(new DummyFinishHandler());
438: registry.loadProductRegistry(registryXml);
439:
440: final Group existing = registry.getGroup(uid);
441:
442: if (existing != null) {
443: existing.getParent().removeChild(existing);
444:
445: Queue<RegistryNode> nodes = new LinkedList<RegistryNode>();
446: nodes.offer(existing);
447:
448: while (nodes.peek() != null) {
449: RegistryNode node = nodes.poll();
450:
451: if (node instanceof Product) {
452: Product temp = (Product) node;
453: FileUtils.deleteFile(new File(productsDir, temp
454: .getUid()
455: + "/" + temp.getVersion()));
456: }
457:
458: if (node instanceof Group) {
459: Group temp = (Group) node;
460: FileUtils.deleteFile(new File(groupsDir, temp
461: .getUid()));
462: }
463:
464: for (RegistryNode child : node.getChildren()) {
465: nodes.offer(child);
466: }
467: }
468: }
469:
470: registry.saveProductRegistry(registryXml,
471: TrueFilter.INSTANCE, true, true, true);
472: } catch (InitializationException e) {
473: e.printStackTrace();
474: throw new ManagerException("Could not remove component", e);
475: } catch (FinalizationException e) {
476: e.printStackTrace();
477: throw new ManagerException("Could not remove component", e);
478: } catch (IOException e) {
479: e.printStackTrace();
480: throw new ManagerException("Could not load registry", e);
481: }
482: }
483:
484: // miscellanea //////////////////////////////////////////////////////////////////
485: public File exportRegistries(String[] registryNames, String codebase)
486: throws ManagerException {
487: try {
488: final File userDir = FileUtils.createTempFile(TEMP, false);
489: final File registryFile = FileUtils.createTempFile(TEMP,
490: false);
491:
492: final File target = new File(EXPORTED, StringUtils
493: .asString(registryNames, ", ")
494: + ".jar");
495:
496: FileUtils.mkdirs(target.getParentFile());
497:
498: final Registry registry = new Registry();
499:
500: registry.setLocalDirectory(userDir);
501: registry.setFinishHandler(new DummyFinishHandler());
502:
503: for (String registryName : registryNames) {
504: registry.loadProductRegistry(new File(registries
505: .get(registryName), REGISTRY_XML));
506: }
507:
508: registry.saveProductRegistry(registryFile,
509: TrueFilter.INSTANCE, false, true, true);
510:
511: final JarOutputStream out = new JarOutputStream(
512: new FileOutputStream(target));
513:
514: for (String registryName : registryNames) {
515: final List<File> excludes = new LinkedList<File>();
516:
517: excludes.add(new File(registries.get(registryName),
518: REGISTRY_XML));
519:
520: FileUtils.zip(registries.get(registryName), out,
521: registries.get(registryName).getParentFile(),
522: excludes);
523:
524: }
525:
526: FileUtils.modifyFile(registryFile,
527: ">.*?registry=(.+?)&file=", ">" + codebase
528: + "/$1/", true);
529: FileUtils.modifyFile(registryFile, "%2F", "/");
530: FileUtils.modifyFile(registryFile, "+", "%20");
531:
532: out.putNextEntry(new ZipEntry("registry.xml"));
533: StreamUtils.transferFile(registryFile, out);
534:
535: out.putNextEntry(new ZipEntry("nbi-engine.jar"));
536: StreamUtils.transferFile(getEngine(), out);
537:
538: out.putNextEntry(new ZipEntry("nbi.jnlp"));
539: StreamUtils.writeChars(out, StringUtils.format(JNLP_STUB,
540: codebase, "nbi.jnlp", codebase + "/nbi-engine.jar",
541: codebase + "/registry.xml"));
542:
543: FileUtils.deleteFile(userDir);
544: FileUtils.deleteFile(registryFile);
545:
546: out.close();
547:
548: return target;
549: } catch (IOException e) {
550: throw new ManagerException("Cannot export", e);
551: } catch (InitializationException e) {
552: throw new ManagerException("Cannot export", e);
553: } catch (FinalizationException e) {
554: throw new ManagerException("Cannot export", e);
555: }
556: }
557:
558: public String getJnlp(String[] registryNames, String codebase)
559: throws ManagerException {
560: try {
561: String jnlp = "install?true=true";
562: for (String registryName : registryNames) {
563: jnlp += "®istry="
564: + URLEncoder.encode(registryName, "UTF-8");
565: }
566:
567: String engine = codebase + "/nbi-engine.jar";
568:
569: String registry = "";
570: for (String registryName : registryNames) {
571: registry += codebase + "/get-registry?registry="
572: + URLEncoder.encode(registryName, "UTF-8")
573: + "\n";
574: }
575: registry = registry.trim();
576:
577: return StringUtils.format(JNLP_STUB, codebase, jnlp,
578: engine, registry);
579: } catch (UnsupportedEncodingException e) {
580: throw new ManagerException("Whoah..", e);
581: }
582: }
583:
584: public File getFile(String registryName, String file)
585: throws ManagerException {
586: if (registries.get(registryName) == null) {
587: addRegistry(registryName);
588: }
589:
590: final File registryDir = registries.get(registryName);
591:
592: return new File(registryDir, file);
593: }
594:
595: public Registry loadRegistry(String... registryNames)
596: throws ManagerException {
597: if (registryNames.length > 0) {
598: List<File> files = new LinkedList<File>();
599:
600: for (String name : registryNames) {
601: if (registries.get(name) == null) {
602: addRegistry(name);
603: }
604:
605: files.add(new File(registries.get(name), REGISTRY_XML));
606: }
607:
608: try {
609: final Registry registry = new Registry();
610:
611: registry.setLocalDirectory(NBI);
612: registry.setFinishHandler(new DummyFinishHandler());
613: for (File file : files) {
614: registry.loadProductRegistry(file);
615: }
616:
617: return registry;
618: } catch (InitializationException e) {
619: e.printStackTrace();
620: throw new ManagerException("Could not load registry", e);
621: }
622: }
623:
624: return null;
625: }
626:
627: public List<Product> getProducts(String... registryNames)
628: throws ManagerException {
629: List<Product> components = new LinkedList<Product>();
630:
631: if (registryNames.length > 0) {
632: final List<File> files = new LinkedList<File>();
633: for (String registryName : registryNames) {
634: if (registries.get(registryName) == null) {
635: addRegistry(registryName);
636: }
637:
638: files.add(new File(registries.get(registryName),
639: REGISTRY_XML));
640: }
641:
642: try {
643: final Registry registry = new Registry();
644: registry.setLocalDirectory(NBI);
645: registry.setFinishHandler(new DummyFinishHandler());
646:
647: for (File file : files) {
648: registry.loadProductRegistry(file);
649: }
650:
651: components.addAll(registry.getProducts());
652: } catch (InitializationException e) {
653: e.printStackTrace();
654: throw new ManagerException("Could not load registry", e);
655: }
656: }
657:
658: return components;
659: }
660:
661: public File createBundle(Platform platform, String[] registryNames,
662: String[] components) throws ManagerException {
663: bundlesLock.lock();
664: try {
665: final String key = StringUtils.asString(registryNames)
666: + StringUtils.asString(components) + platform;
667:
668: if ((bundles.get(key) != null) && bundles.get(key).exists()) {
669: return bundles.get(key);
670: }
671:
672: if (bundles.get(key) != null) {
673: bundles.remove(key);
674: }
675:
676: try {
677: File statefile = FileUtils.createTempFile(TEMP, false);
678: File userDir = FileUtils.createTempFile(TEMP, false);
679: File bundle = new File(FileUtils.createTempFile(
680: BUNDLES, false), "bundle.jar");
681:
682: File javaHome = new File(System
683: .getProperty("java.home"));
684:
685: String remote = "";
686: List<File> files = new LinkedList<File>();
687: for (String name : registryNames) {
688: if (registries.get(name) == null) {
689: addRegistry(name);
690: }
691: File xml = new File(registries.get(name),
692: REGISTRY_XML);
693:
694: files.add(xml);
695: remote += xml.toURI().toString() + "\n";
696: }
697: remote = remote.trim();
698:
699: final Registry registry = new Registry();
700:
701: registry.setLocalDirectory(NBI);
702: registry.setFinishHandler(new DummyFinishHandler());
703: registry.setTargetPlatform(platform);
704: for (File file : files) {
705: registry.loadProductRegistry(file);
706: }
707:
708: for (String string : components) {
709: String[] parts = string.split(",");
710:
711: registry.getProduct(parts[0],
712: Version.getVersion(parts[1])).setStatus(
713: Status.INSTALLED);
714: }
715: registry.saveStateFile(statefile, new Progress());
716:
717: bundle.getParentFile().mkdirs();
718:
719: ExecutionResults results = SystemUtils.executeCommand(
720: JavaUtils.getExecutable(javaHome)
721: .getAbsolutePath(),
722: "-Dnbi.product.remote.registries=" + remote,
723: "-jar", ENGINE.getAbsolutePath(), "--silent",
724: "--state", statefile.getAbsolutePath(),
725: "--create-bundle", bundle.getAbsolutePath(),
726: "--ignore-lock", "--platform", platform
727: .toString(), "--userdir", userDir
728: .getAbsolutePath());
729:
730: System.out.println(results.getErrorCode());
731: System.out.println(results.getStdOut());
732: System.out.println(results.getStdErr());
733:
734: if (results.getErrorCode() != 0) {
735: throw new ManagerException(
736: "Could not create bundle - error in running the engine");
737: }
738:
739: FileUtils.deleteFile(statefile);
740: FileUtils.deleteFile(userDir, true);
741:
742: if (platform == Platform.WINDOWS) {
743: bundle = new File(bundle.getAbsolutePath()
744: .replaceFirst("\\.jar$", ".exe"));
745: } else if (platform.isCompatibleWith(Platform.MACOSX)) {
746: bundle = new File(bundle.getAbsolutePath()
747: .replaceFirst("\\.jar$", ".zip"));
748: } else {
749: bundle = new File(bundle.getAbsolutePath()
750: .replaceFirst("\\.jar$", ".sh"));
751: }
752:
753: bundles.put(key, bundle);
754:
755: return bundle;
756: } catch (InitializationException e) {
757: e.printStackTrace();
758: throw new ManagerException("Could not load registry", e);
759: } catch (FinalizationException e) {
760: e.printStackTrace();
761: throw new ManagerException("Could not load registry", e);
762: } catch (IOException e) {
763: e.printStackTrace();
764: throw new ManagerException("Could not load registry", e);
765: }
766: } finally {
767: bundlesLock.unlock();
768: }
769: }
770:
771: public void generateBundles(String[] registryNames)
772: throws ManagerException {
773: try {
774: final List<File> files = new ArrayList<File>();
775: for (String name : registryNames) {
776: if (registries.get(name) == null) {
777: addRegistry(name);
778: }
779:
780: files.add(new File(registries.get(name), REGISTRY_XML));
781: }
782:
783: for (Platform platform : Platform.values()) {
784: final Registry registry = new Registry();
785:
786: registry.setLocalDirectory(NBI);
787: registry.setFinishHandler(new DummyFinishHandler());
788: for (File file : files) {
789: registry.loadProductRegistry(file);
790: }
791:
792: final List<Product> products = registry
793: .getProducts(platform);
794:
795: for (int i = 1; i <= products.size(); i++) {
796: Product[] combination = new Product[i];
797:
798: iterate(platform, registryNames, registry,
799: combination, 0, products, 0);
800: }
801: }
802: } catch (InitializationException e) {
803: throw new ManagerException("Cannot generate bundles", e);
804: }
805: }
806:
807: public void deleteBundles() throws ManagerException {
808: bundlesLock.lock();
809: try {
810: for (String key : bundles.keySet()) {
811: FileUtils.deleteFile(bundles.get(key));
812: }
813:
814: bundles.clear();
815: } catch (IOException e) {
816: throw new ManagerException("Cannot clear bundles", e);
817: } finally {
818: bundlesLock.unlock();
819: }
820: }
821:
822: // private //////////////////////////////////////////////////////////////////////
823: private void loadRegistriesList() throws ManagerException {
824: try {
825: BufferedReader reader = new BufferedReader(
826: new InputStreamReader(new FileInputStream(
827: REGISTRIES_LIST)));
828:
829: String line = null;
830: while ((line = reader.readLine()) != null) {
831: line = line.trim();
832: registries.put(line, initializeRegistry(line));
833: }
834:
835: reader.close();
836: } catch (IOException e) {
837: e.printStackTrace();
838: throw new ManagerException("Could not load registry", e);
839: }
840: }
841:
842: private void saveRegistriesList() throws ManagerException {
843: try {
844: PrintWriter writer = new PrintWriter(
845: new OutputStreamWriter(new FileOutputStream(
846: REGISTRIES_LIST)));
847:
848: try {
849: for (String key : registries.keySet()) {
850: writer.println(key);
851: }
852: } finally {
853: writer.close();
854: }
855: } catch (IOException e) {
856: e.printStackTrace();
857: throw new ManagerException("Could not load registry", e);
858: }
859: }
860:
861: private File initializeRegistry(String name)
862: throws ManagerException {
863: File directory = new File(REGISTRIES, name);
864: File registryxml = new File(directory, REGISTRY_XML);
865:
866: directory.mkdirs();
867:
868: if (!registryxml.exists()) {
869: try {
870: Document document = Registry.getInstance()
871: .getEmptyRegistryDocument();
872: XMLUtils.saveXMLDocument(document, registryxml);
873: } catch (XMLException e) {
874: e.printStackTrace();
875: throw new ManagerException(
876: "Cannot initialize registry", e);
877: }
878: }
879:
880: return directory;
881: }
882:
883: private void iterate(Platform platform, String[] registryNames,
884: Registry registry, Product[] combination, int index,
885: List<Product> products, int start) throws ManagerException {
886: for (int i = start; i < products.size(); i++) {
887: combination[index] = products.get(i);
888:
889: if (index == combination.length - 1) {
890: for (Product product : products) {
891: product.setStatus(Status.NOT_INSTALLED);
892: }
893: for (Product product : combination) {
894: product.setStatus(Status.TO_BE_INSTALLED);
895: }
896:
897: if (registry.getProductsToInstall().size() == combination.length) {
898: String[] components = new String[combination.length];
899:
900: for (int j = 0; j < combination.length; j++) {
901: components[j] = combination[j].getUid()
902: + ","
903: + combination[j].getVersion()
904: .toString();
905: }
906:
907: createBundle(platform, registryNames, components);
908: }
909: } else {
910: iterate(platform, registryNames, registry, combination,
911: index + 1, products, i + 1);
912: }
913: }
914: }
915: }
|