0001: /*
0002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0003: *
0004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
0005: *
0006: * The contents of this file are subject to the terms of either the GNU General
0007: * Public License Version 2 only ("GPL") or the Common Development and Distribution
0008: * License("CDDL") (collectively, the "License"). You may not use this file except in
0009: * compliance with the License. You can obtain a copy of the License at
0010: * http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the
0011: * License for the specific language governing permissions and limitations under the
0012: * License. When distributing the software, include this License Header Notice in
0013: * each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun
0014: * designates this particular file as subject to the "Classpath" exception as
0015: * provided by Sun in the GPL Version 2 section of the License file that
0016: * accompanied this code. If applicable, add the following below the License Header,
0017: * with the fields enclosed by brackets [] replaced by your own identifying
0018: * information: "Portions Copyrighted [year] [name of copyright owner]"
0019: *
0020: * Contributor(s):
0021: *
0022: * The Original Software is NetBeans. The Initial Developer of the Original Software
0023: * is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun Microsystems, Inc. All
0024: * Rights Reserved.
0025: *
0026: * If you wish your version of this file to be governed by only the CDDL or only the
0027: * GPL Version 2, indicate your decision by adding "[Contributor] elects to include
0028: * this software in this distribution under the [CDDL or GPL Version 2] license." If
0029: * you do not indicate a single choice of license, a recipient has the option to
0030: * distribute your version of this file under either the CDDL, the GPL Version 2 or
0031: * to extend the choice of license to its licensees as provided above. However, if
0032: * you add GPL Version 2 code and therefore, elected the GPL Version 2 license, then
0033: * the option applies only if the new code is made subject to such option by the
0034: * copyright holder.
0035: */
0036:
0037: package org.netbeans.installer.infra.lib.registries.impl;
0038:
0039: import java.io.File;
0040: import java.io.FileOutputStream;
0041: import java.io.IOException;
0042: import java.io.OutputStream;
0043: import java.util.Arrays;
0044: import java.util.HashMap;
0045: import java.util.LinkedList;
0046: import java.util.List;
0047: import java.util.Locale;
0048: import java.util.Map;
0049: import java.util.Properties;
0050: import java.util.Queue;
0051: import java.util.concurrent.locks.ReentrantLock;
0052: import org.netbeans.installer.Installer;
0053: import org.netbeans.installer.downloader.DownloadManager;
0054: import org.netbeans.installer.product.Registry;
0055: import org.netbeans.installer.product.RegistryNode;
0056: import org.netbeans.installer.product.components.Group;
0057: import org.netbeans.installer.product.components.Product;
0058: import org.netbeans.installer.product.filters.TrueFilter;
0059: import org.netbeans.installer.utils.FileUtils;
0060: import org.netbeans.installer.utils.LogManager;
0061: import org.netbeans.installer.utils.StringUtils;
0062: import org.netbeans.installer.utils.SystemUtils;
0063: import org.netbeans.installer.utils.applications.JavaUtils;
0064: import org.netbeans.installer.utils.exceptions.FinalizationException;
0065: import org.netbeans.installer.utils.exceptions.InitializationException;
0066: import org.netbeans.installer.utils.exceptions.ParseException;
0067: import org.netbeans.installer.utils.exceptions.XMLException;
0068: import org.netbeans.installer.utils.helper.ExecutionResults;
0069: import org.netbeans.installer.utils.helper.Platform;
0070: import org.netbeans.installer.utils.helper.Status;
0071: import org.netbeans.installer.utils.helper.Version;
0072: import org.netbeans.installer.infra.lib.registries.ManagerException;
0073: import org.netbeans.installer.infra.lib.registries.RegistriesManager;
0074: import org.netbeans.installer.utils.progress.Progress;
0075:
0076: /**
0077: *
0078: * @author Kirill Sorokin
0079: */
0080: public class RegistriesManagerImpl implements RegistriesManager {
0081: /////////////////////////////////////////////////////////////////////////////////
0082: // Static
0083: private static Map<File, ReentrantLock> locks = new HashMap<File, ReentrantLock>();
0084:
0085: /////////////////////////////////////////////////////////////////////////////////
0086: // Instance
0087:
0088: // engine operations ////////////////////////////////////////////////////////////
0089: public File getEngine(final File root) throws ManagerException {
0090: final ReentrantLock lock = getLock(root);
0091:
0092: try {
0093: lock.lock();
0094:
0095: initializeRegistryNoLock(root);
0096:
0097: return new File(root, ENGINE_JAR);
0098: } finally {
0099: lock.unlock();
0100: }
0101: }
0102:
0103: public void updateEngine(final File root, final File archive)
0104: throws ManagerException {
0105: final ReentrantLock lock = getLock(root);
0106:
0107: try {
0108: lock.lock();
0109:
0110: initializeRegistryNoLock(root);
0111: deleteBundles(root);
0112:
0113: FileUtils.copyFile(archive, new File(root, ENGINE_JAR));
0114: } catch (IOException e) {
0115: throw new ManagerException(e);
0116: } finally {
0117: lock.unlock();
0118: }
0119: }
0120:
0121: // component operations /////////////////////////////////////////////////////////
0122: public void addPackage(final File root, final File archive,
0123: final String parentUid, final String parentVersion,
0124: final String parentPlatforms) throws ManagerException {
0125: final ReentrantLock lock = getLock(root);
0126:
0127: try {
0128: lock.lock();
0129:
0130: initializeRegistryNoLock(root);
0131: deleteBundles(root);
0132:
0133: final File tempDir = FileUtils.createTempFile(new File(
0134: root, TEMP), false);
0135:
0136: final File registryXml = new File(root, REGISTRY_XML);
0137: final File componentsDir = new File(root, COMPONENTS);
0138: final File archiveDir = FileUtils.createTempFile(new File(
0139: root, TEMP), false);
0140: final File archiveRegistryXml = new File(archiveDir,
0141: REGISTRY_XML);
0142:
0143: FileUtils.unjar(archive, archiveDir);
0144: FileUtils.modifyFile(archiveRegistryXml,
0145: "(\\>)resource:(.*?\\<\\/)", "$1"
0146: + componentsDir.toURI() + "$2", true);
0147:
0148: final Registry registry = new Registry();
0149: registry.setLocalDirectory(tempDir);
0150: registry.setFinishHandler(DummyFinishHandler.INSTANCE);
0151: registry.loadProductRegistry(registryXml);
0152:
0153: final Registry archiveRegistry = new Registry();
0154: archiveRegistry.setLocalDirectory(tempDir);
0155: archiveRegistry
0156: .setFinishHandler(DummyFinishHandler.INSTANCE);
0157: archiveRegistry.loadProductRegistry(archiveRegistryXml);
0158:
0159: final Queue<RegistryNode> nodes = new LinkedList<RegistryNode>();
0160:
0161: for (Product product : archiveRegistry.getProducts()) {
0162: final List<Product> existingProducts = registry
0163: .getProducts(product.getUid(), product
0164: .getVersion(), product.getPlatforms());
0165:
0166: if (existingProducts.size() > 0) {
0167: for (Product existingProduct : existingProducts) {
0168: nodes.offer(existingProduct);
0169: }
0170: }
0171: }
0172:
0173: for (Group group : archiveRegistry.getGroups()) {
0174: if (!group.equals(archiveRegistry.getRegistryRoot())) {
0175: final Group existingGroup = registry.getGroup(group
0176: .getUid());
0177:
0178: if (existingGroup != null) {
0179: nodes.offer(existingGroup);
0180: }
0181: }
0182: }
0183:
0184: if (nodes.size() > 0) {
0185: while (nodes.peek() != null) {
0186: final RegistryNode node = nodes.poll();
0187:
0188: node.getParent().removeChild(node);
0189:
0190: if (node instanceof Product) {
0191: final Product temp = (Product) node;
0192: final String path = PRODUCTS
0193: + "/"
0194: + temp.getUid()
0195: + "/"
0196: + temp.getVersion()
0197: + "/"
0198: + StringUtils.asString(temp
0199: .getPlatforms(), " ");
0200:
0201: FileUtils
0202: .deleteFile(new File(root, path), true);
0203: }
0204:
0205: if (node instanceof Group) {
0206: final Group temp = (Group) node;
0207: final String path = GROUPS + "/"
0208: + temp.getUid();
0209:
0210: FileUtils
0211: .deleteFile(new File(root, path), true);
0212: }
0213:
0214: for (RegistryNode child : node.getChildren()) {
0215: nodes.offer(child);
0216: }
0217: }
0218: }
0219:
0220: final File productsDir = new File(root, PRODUCTS);
0221: FileUtils.mkdirs(productsDir);
0222: FileUtils.copyFile(new File(archiveDir, "products"), // temp hack
0223: productsDir, true);
0224:
0225: final File groupsDir = new File(root, GROUPS);
0226: FileUtils.mkdirs(groupsDir);
0227: FileUtils.copyFile(new File(archiveDir, "groups"), // temp hack
0228: groupsDir, true);
0229:
0230: RegistryNode parent;
0231:
0232: List<Product> parents = null;
0233: if ((parentVersion != null)
0234: && !parentVersion.equals("null")
0235: && (parentPlatforms != null)
0236: && !parentPlatforms.equals("null")) {
0237: parents = registry.getProducts(parentUid, Version
0238: .getVersion(parentVersion), StringUtils
0239: .parsePlatforms(parentPlatforms));
0240: }
0241: if ((parents == null) || (parents.size() == 0)) {
0242: parent = registry.getGroup(parentUid);
0243: if (parent == null) {
0244: parent = registry.getRegistryRoot();
0245: }
0246: } else {
0247: parent = parents.get(0);
0248: }
0249:
0250: parent.attachRegistry(archiveRegistry);
0251:
0252: registry.saveProductRegistry(registryXml,
0253: TrueFilter.INSTANCE, true, true, true);
0254:
0255: FileUtils.deleteFile(archiveDir, true);
0256: FileUtils.deleteFile(tempDir, true);
0257: } catch (IOException e) {
0258: throw new ManagerException(e);
0259: } catch (InitializationException e) {
0260: throw new ManagerException(e);
0261: } catch (ParseException e) {
0262: throw new ManagerException(e);
0263: } catch (XMLException e) {
0264: throw new ManagerException(e);
0265: } catch (FinalizationException e) {
0266: throw new ManagerException(e);
0267: } finally {
0268: lock.unlock();
0269: }
0270: }
0271:
0272: public void removeProduct(final File root, final String uid,
0273: final String version, final String platforms)
0274: throws ManagerException {
0275: final ReentrantLock lock = getLock(root);
0276:
0277: try {
0278: lock.lock();
0279:
0280: initializeRegistryNoLock(root);
0281: deleteBundles(root);
0282:
0283: final File tempDir = FileUtils.createTempFile(new File(
0284: root, TEMP), false);
0285:
0286: final File registryXml = new File(root, REGISTRY_XML);
0287: final File productsDir = new File(root, PRODUCTS);
0288: final File groupsDir = new File(root, GROUPS);
0289:
0290: final Registry registry = new Registry();
0291:
0292: registry.setLocalDirectory(tempDir);
0293: registry.setFinishHandler(DummyFinishHandler.INSTANCE);
0294: registry.loadProductRegistry(registryXml);
0295:
0296: final List<Product> existing = registry.getProducts(uid,
0297: Version.getVersion(version), StringUtils
0298: .parsePlatforms(platforms));
0299:
0300: if (existing != null) {
0301: existing.get(0).getParent()
0302: .removeChild(existing.get(0));
0303:
0304: final Queue<RegistryNode> nodes = new LinkedList<RegistryNode>();
0305: nodes.offer(existing.get(0));
0306:
0307: while (nodes.peek() != null) {
0308: final RegistryNode node = nodes.poll();
0309:
0310: if (node instanceof Product) {
0311: final Product product = (Product) node;
0312:
0313: final File uidDir = new File(productsDir,
0314: product.getUid());
0315: final File versionDir = new File(uidDir,
0316: product.getVersion().toString());
0317: final File platformsDir = new File(versionDir,
0318: StringUtils.asString(product
0319: .getPlatforms(), " "));
0320:
0321: FileUtils.deleteFile(platformsDir, true);
0322: if (FileUtils.isEmpty(versionDir)) {
0323: FileUtils.deleteFile(versionDir, true);
0324: }
0325: if (FileUtils.isEmpty(uidDir)) {
0326: FileUtils.deleteFile(uidDir, true);
0327: }
0328:
0329: }
0330:
0331: if (node instanceof Group) {
0332: final Group group = (Group) node;
0333:
0334: final File uidDir = new File(groupsDir, group
0335: .getUid());
0336:
0337: FileUtils.deleteFile(uidDir, true);
0338: }
0339:
0340: for (RegistryNode child : node.getChildren()) {
0341: nodes.offer(child);
0342: }
0343: }
0344: }
0345:
0346: registry.saveProductRegistry(registryXml,
0347: TrueFilter.INSTANCE, true, true, true);
0348:
0349: } catch (IOException e) {
0350: throw new ManagerException(e);
0351: } catch (InitializationException e) {
0352: throw new ManagerException(e);
0353: } catch (ParseException e) {
0354: throw new ManagerException(e);
0355: } catch (FinalizationException e) {
0356: throw new ManagerException(e);
0357: } finally {
0358: lock.unlock();
0359: }
0360: }
0361:
0362: public void removeGroup(final File root, final String uid)
0363: throws ManagerException {
0364: final ReentrantLock lock = getLock(root);
0365:
0366: try {
0367: lock.lock();
0368:
0369: initializeRegistryNoLock(root);
0370: deleteBundles(root);
0371:
0372: final File tempDir = FileUtils.createTempFile(new File(
0373: root, TEMP), false);
0374:
0375: final File registryXml = new File(root, REGISTRY_XML);
0376: final File productsDir = new File(root, PRODUCTS);
0377: final File groupsDir = new File(root, GROUPS);
0378:
0379: final Registry registry = new Registry();
0380:
0381: registry.setLocalDirectory(tempDir);
0382: registry.setFinishHandler(DummyFinishHandler.INSTANCE);
0383: registry.loadProductRegistry(registryXml);
0384:
0385: final Group existing = registry.getGroup(uid);
0386: if (existing != null) {
0387: existing.getParent().removeChild(existing);
0388:
0389: Queue<RegistryNode> nodes = new LinkedList<RegistryNode>();
0390: nodes.offer(existing);
0391:
0392: while (nodes.peek() != null) {
0393: final RegistryNode node = nodes.poll();
0394:
0395: if (node instanceof Product) {
0396: final Product product = (Product) node;
0397:
0398: final File uidDir = new File(productsDir,
0399: product.getUid());
0400: final File versionDir = new File(uidDir,
0401: product.getVersion().toString());
0402: final File platformsDir = new File(versionDir,
0403: StringUtils.asString(product
0404: .getPlatforms(), " "));
0405:
0406: FileUtils.deleteFile(platformsDir, true);
0407: if (FileUtils.isEmpty(versionDir)) {
0408: FileUtils.deleteFile(versionDir, true);
0409: }
0410: if (FileUtils.isEmpty(uidDir)) {
0411: FileUtils.deleteFile(uidDir, true);
0412: }
0413:
0414: }
0415:
0416: if (node instanceof Group) {
0417: final Group group = (Group) node;
0418:
0419: final File uidDir = new File(groupsDir, group
0420: .getUid());
0421:
0422: FileUtils.deleteFile(uidDir, true);
0423: }
0424:
0425: for (RegistryNode child : node.getChildren()) {
0426: nodes.offer(child);
0427: }
0428: }
0429: }
0430:
0431: registry.saveProductRegistry(registryXml,
0432: TrueFilter.INSTANCE, true, true, true);
0433: } catch (IOException e) {
0434: throw new ManagerException(e);
0435: } catch (InitializationException e) {
0436: throw new ManagerException(e);
0437: } catch (FinalizationException e) {
0438: throw new ManagerException(e);
0439: } finally {
0440: lock.unlock();
0441: }
0442: }
0443:
0444: // bundles //////////////////////////////////////////////////////////////////////
0445: public File createBundle(final File root, final Platform platform,
0446: final String[] components, Properties props,
0447: Properties bundleProps) throws ManagerException {
0448: final ReentrantLock lock = getLock(root);
0449:
0450: try {
0451: lock.lock();
0452:
0453: initializeRegistryNoLock(root);
0454:
0455: return createBundleNoLock(root, platform, components,
0456: props, bundleProps);
0457: } finally {
0458: lock.unlock();
0459: }
0460: }
0461:
0462: // bundles //////////////////////////////////////////////////////////////////////
0463: public File createBundle(final File root, final Platform platform,
0464: final String[] components) throws ManagerException {
0465: return createBundle(root, platform, components,
0466: new Properties(), new Properties());
0467: }
0468:
0469: public void generateBundles(final File root)
0470: throws ManagerException {
0471: final ReentrantLock lock = getLock(root);
0472:
0473: try {
0474: lock.lock();
0475:
0476: initializeRegistryNoLock(root);
0477:
0478: final File tempDir = FileUtils.createTempFile(new File(
0479: root, TEMP), false);
0480:
0481: final File registryXml = new File(root, REGISTRY_XML);
0482:
0483: for (Platform platform : Platform.values()) {
0484: final Registry registry = new Registry();
0485:
0486: registry.setLocalDirectory(tempDir);
0487: registry.setFinishHandler(DummyFinishHandler.INSTANCE);
0488: registry.loadProductRegistry(registryXml);
0489:
0490: final List<Product> products = registry
0491: .getProducts(platform);
0492: for (int i = 1; i <= products.size(); i++) {
0493: final Product[] combination = new Product[i];
0494:
0495: iterate(platform, root, registry, combination, 0,
0496: products, 0);
0497: }
0498: }
0499: } catch (IOException e) {
0500: throw new ManagerException(e);
0501: } catch (InitializationException e) {
0502: throw new ManagerException(e);
0503: } finally {
0504: lock.unlock();
0505: }
0506: }
0507:
0508: public void deleteBundles(final File root) throws ManagerException {
0509: final ReentrantLock lock = getLock(root);
0510:
0511: try {
0512: lock.lock();
0513:
0514: initializeRegistryNoLock(root);
0515:
0516: deleteBudlesNoLock(root);
0517: } finally {
0518: lock.unlock();
0519: }
0520: }
0521:
0522: // miscellanea //////////////////////////////////////////////////////////////////
0523: public void initializeRegistry(final File root)
0524: throws ManagerException {
0525: final ReentrantLock lock = getLock(root);
0526:
0527: try {
0528: lock.lock();
0529:
0530: initializeRegistryNoLock(root);
0531: } finally {
0532: lock.unlock();
0533: }
0534: }
0535:
0536: public File exportRegistry(final File root, final File destination,
0537: final String codebase) throws ManagerException {
0538: final ReentrantLock lock = getLock(root);
0539:
0540: try {
0541: lock.lock();
0542:
0543: initializeRegistryNoLock(root);
0544:
0545: final File registryXml = new File(root, REGISTRY_XML);
0546:
0547: final File tempDir = FileUtils.createTempFile(new File(
0548: root, TEMP), true, true);
0549: final File tempUserDir = FileUtils.createTempFile(tempDir,
0550: false);
0551: final File tempRegistryXml = FileUtils.createTempFile(
0552: tempDir, false);
0553:
0554: FileUtils.mkdirs(destination.getParentFile());
0555:
0556: final Registry registry = new Registry();
0557: registry.setLocalDirectory(tempUserDir);
0558: registry.setFinishHandler(DummyFinishHandler.INSTANCE);
0559: registry.loadProductRegistry(registryXml);
0560:
0561: registry.saveProductRegistry(tempRegistryXml,
0562: TrueFilter.INSTANCE, false, true, true);
0563:
0564: FileUtils.copyFile(new File(root, COMPONENTS), new File(
0565: destination, COMPONENTS), true);
0566:
0567: final String replacement = codebase.endsWith("/") ? codebase
0568: : codebase + "/";
0569: FileUtils.modifyFile(tempRegistryXml, "uri>"
0570: + root.toURI().toString(), "uri>" + replacement);
0571:
0572: FileUtils.copyFile(tempRegistryXml, new File(destination,
0573: REGISTRY_XML));
0574: FileUtils.copyFile(new File(root, ENGINE_JAR), new File(
0575: destination, ENGINE_JAR));
0576:
0577: FileUtils.deleteFile(tempUserDir);
0578: FileUtils.deleteFile(tempRegistryXml);
0579:
0580: return destination;
0581: } catch (IOException e) {
0582: throw new ManagerException(e);
0583: } catch (InitializationException e) {
0584: throw new ManagerException(e);
0585: } catch (FinalizationException e) {
0586: throw new ManagerException(e);
0587: } finally {
0588: lock.unlock();
0589: }
0590: }
0591:
0592: public String generateComponentsJs(final File root)
0593: throws ManagerException {
0594: return generateComponentsJs(root, null);
0595: }
0596:
0597: public String generateComponentsJs(final File root,
0598: final String localeString) throws ManagerException {
0599: final List<String> java = Arrays.asList("nb-platform",
0600: "nb-base", "nb-javase");
0601: final List<String> javaee = Arrays.asList("nb-platform",
0602: "nb-base", "nb-javase", "nb-javaee", "glassfish",
0603: "tomcat", "sjsas");
0604: final List<String> javame = Arrays.asList("nb-platform",
0605: "nb-base", "nb-javase", "nb-javame");
0606: final List<String> ruby = Arrays.asList("nb-platform",
0607: "nb-base", "nb-ruby");
0608:
0609: final List<String> cnd = Arrays.asList("nb-platform",
0610: "nb-base", "nb-cnd");
0611:
0612: final List<String> full = Arrays.asList("nb-platform",
0613: "nb-base", "nb-javase", "nb-javaee", "nb-javame",
0614: "nb-cnd", "nb-soa", "nb-uml", "nb-ruby", "nb-php",
0615: "glassfish", "openesb", "sjsam", "tomcat", "sjsas");
0616:
0617: final List<String> hidden = Arrays.asList("nb-platform",
0618: //"nb-base",
0619: "openesb", "sjsam", "jdk");
0620: final Map<String, String> notes = new HashMap<String, String>();
0621: //notes.put("nb-javase", "for Java SE, includes GUI Builder, Profiler");
0622:
0623: Locale locale = StringUtils
0624: .parseLocale(localeString == null ? "" : localeString);
0625:
0626: final ReentrantLock lock = getLock(root);
0627:
0628: try {
0629: lock.lock();
0630:
0631: final File tempDir = FileUtils.createTempFile(new File(
0632: root, TEMP), true, true);
0633: final File tempUserDir = FileUtils.createTempFile(tempDir,
0634: true, true);
0635:
0636: final StringBuilder out = new StringBuilder();
0637:
0638: final Registry registry = loadRegistry(root, tempUserDir,
0639: Platform.WINDOWS);
0640:
0641: final List<Product> products = getProducts(registry
0642: .getRegistryRoot());
0643: final List<Group> groups = getGroups(registry
0644: .getRegistryRoot());
0645:
0646: final Map<Integer, Integer> productMapping = new HashMap<Integer, Integer>();
0647:
0648: final List<String> productUids = new LinkedList<String>();
0649: final List<String> productVersions = new LinkedList<String>();
0650: final List<String> productDisplayNames = new LinkedList<String>();
0651: final List<String> productNotes = new LinkedList<String>();
0652: final List<String> productDescriptions = new LinkedList<String>();
0653: final List<String> productDownloadSizes = new LinkedList<String>();
0654: final List<List<Platform>> productPlatforms = new LinkedList<List<Platform>>();
0655: final List<String> productProperties = new LinkedList<String>();
0656:
0657: final List<Integer> defaultGroupProducts = new LinkedList<Integer>();
0658: final List<List<Integer>> groupProducts = new LinkedList<List<Integer>>();
0659: final List<String> groupDisplayNames = new LinkedList<String>();
0660: final List<String> groupDescriptions = new LinkedList<String>();
0661:
0662: for (int i = 0; i < products.size(); i++) {
0663: final Product product = products.get(i);
0664:
0665: boolean existingFound = false;
0666: for (int j = 0; j < productUids.size(); j++) {
0667: if (productUids.get(j).equals(product.getUid())
0668: && productVersions.get(j).equals(
0669: product.getVersion().toString())) {
0670: productPlatforms.get(j).addAll(
0671: product.getPlatforms());
0672: productMapping.put(i, j);
0673: existingFound = true;
0674: break;
0675: }
0676: }
0677:
0678: if (existingFound) {
0679: continue;
0680: }
0681:
0682: long size = (long) Math.ceil(((double) product
0683: .getDownloadSize()) / 1024.);
0684: productUids.add(product.getUid());
0685: productVersions.add(product.getVersion().toString());
0686: productDisplayNames.add(product.getDisplayName(locale)
0687: .replace("\"", "\\\"").replaceAll("\r\n|\r|\n",
0688: "\\\n"));
0689: productDescriptions.add(product.getDescription(locale)
0690: .replace("\"", "\\\"").replaceAll("\r\n|\r|\n",
0691: "\\\n"));
0692: productDownloadSizes.add(Long.toString(size));
0693: productPlatforms.add(product.getPlatforms());
0694:
0695: if (notes.get(product.getUid()) != null) {
0696: productNotes.add(notes.get(product.getUid())
0697: .replace("\"", "\\\"").replaceAll(
0698: "\r\n|\r|\n", "\\\n"));
0699: } else {
0700: productNotes.add("");
0701: }
0702:
0703: String properties = "PROPERTY_NONE";
0704: if (java.contains(product.getUid())) {
0705: properties += " | PROPERTY_JAVA";
0706: }
0707: if (javaee.contains(product.getUid())) {
0708: properties += " | PROPERTY_JAVAEE";
0709: }
0710: if (javame.contains(product.getUid())) {
0711: properties += " | PROPERTY_JAVAME";
0712: }
0713: if (ruby.contains(product.getUid())) {
0714: properties += " | PROPERTY_RUBY";
0715: }
0716: if (cnd.contains(product.getUid())) {
0717: properties += " | PROPERTY_CND";
0718: }
0719: if (full.contains(product.getUid())) {
0720: properties += " | PROPERTY_FULL";
0721: }
0722: if (hidden.contains(product.getUid())) {
0723: properties += " | PROPERTY_HIDDEN";
0724: }
0725: productProperties.add(properties);
0726:
0727: productMapping.put(i, productUids.size() - 1);
0728: }
0729:
0730: out.append("product_uids = new Array();").append("\n");
0731: for (int i = 0; i < productUids.size(); i++) {
0732: out.append(
0733: " product_uids[" + i + "] = \""
0734: + productUids.get(i) + "\";").append(
0735: "\n");
0736: }
0737: out.append("\n");
0738:
0739: out.append("product_versions = new Array();").append("\n");
0740: for (int i = 0; i < productVersions.size(); i++) {
0741: out.append(
0742: " product_versions[" + i + "] = \""
0743: + productVersions.get(i) + "\";")
0744: .append("\n");
0745: }
0746: out.append("\n");
0747:
0748: out.append("product_display_names = new Array();").append(
0749: "\n");
0750: for (int i = 0; i < productDisplayNames.size(); i++) {
0751: out.append(
0752: " product_display_names[" + i + "] = \""
0753: + productDisplayNames.get(i) + "\";")
0754: .append("\n");
0755: }
0756: out.append("\n");
0757:
0758: out.append("product_notes = new Array();").append("\n");
0759: for (int i = 0; i < productNotes.size(); i++) {
0760: out.append(
0761: " product_notes[" + i + "] = \""
0762: + productNotes.get(i) + "\";").append(
0763: "\n");
0764: }
0765: out.append("\n");
0766:
0767: out.append("product_descriptions = new Array();").append(
0768: "\n");
0769: for (int i = 0; i < productDescriptions.size(); i++) {
0770: out.append(
0771: " product_descriptions[" + i + "] = \""
0772: + productDescriptions.get(i) + "\";")
0773: .append("\n");
0774: }
0775: out.append("\n");
0776:
0777: out.append("product_download_sizes = new Array();").append(
0778: "\n");
0779: for (int i = 0; i < productDownloadSizes.size(); i++) {
0780: out.append(
0781: " product_download_sizes[" + i + "] = "
0782: + productDownloadSizes.get(i) + ";")
0783: .append("\n");
0784: }
0785: out.append("\n");
0786:
0787: out.append("product_platforms = new Array();").append("\n");
0788: for (int i = 0; i < productPlatforms.size(); i++) {
0789: out.append(
0790: " product_platforms[" + i
0791: + "] = new Array();").append("\n");
0792: for (int j = 0; j < productPlatforms.get(i).size(); j++) {
0793: out.append(
0794: " product_platforms[" + i + "][" + j
0795: + "] = \""
0796: + productPlatforms.get(i).get(j)
0797: + "\";").append("\n");
0798: }
0799: }
0800: out.append("\n");
0801:
0802: out.append("product_properties = new Array();")
0803: .append("\n");
0804: for (int i = 0; i < productProperties.size(); i++) {
0805: out.append(
0806: " product_properties[" + i + "] = "
0807: + productProperties.get(i) + ";")
0808: .append("\n");
0809: }
0810: out.append("\n");
0811:
0812: for (int i = 0; i < productUids.size(); i++) {
0813: defaultGroupProducts.add(Integer.valueOf(i));
0814: }
0815:
0816: for (Group group : groups) {
0817: List<Integer> components = new LinkedList<Integer>();
0818: for (int i = 0; i < products.size(); i++) {
0819: if (products.get(i).getParent().equals(group)) {
0820: Integer index = Integer.valueOf(productMapping
0821: .get(i));
0822: if (!components.contains(index)) {
0823: components.add(index);
0824: defaultGroupProducts.remove(index);
0825: }
0826: }
0827: }
0828:
0829: groupProducts.add(components);
0830: groupDisplayNames.add(group.getDisplayName(locale)
0831: .replace("\"", "\\\"").replaceAll("\r\n|\r|\n",
0832: "\\\n"));
0833: groupDescriptions.add(group.getDescription(locale)
0834: .replace("\"", "\\\"").replaceAll("\r\n|\r|\n",
0835: "\\\n"));
0836: }
0837:
0838: if (defaultGroupProducts.size() > 0) {
0839: groupProducts.add(0, defaultGroupProducts);
0840: groupDisplayNames.add(0, "");
0841: groupDescriptions.add(0, "");
0842: }
0843:
0844: out.append("group_products = new Array();").append("\n");
0845: for (int i = 0; i < groupProducts.size(); i++) {
0846: out.append(
0847: " group_products[" + i + "] = new Array();")
0848: .append("\n");
0849: for (int j = 0; j < groupProducts.get(i).size(); j++) {
0850: out
0851: .append(
0852: " group_products["
0853: + i
0854: + "]["
0855: + j
0856: + "] = "
0857: + groupProducts.get(i).get(
0858: j) + ";").append(
0859: "\n");
0860: }
0861: }
0862: out.append("\n");
0863:
0864: out.append("group_display_names = new Array();").append(
0865: "\n");
0866: for (int i = 0; i < groupDisplayNames.size(); i++) {
0867: out.append(
0868: " group_display_names[" + i + "] = \""
0869: + groupDisplayNames.get(i) + "\";")
0870: .append("\n");
0871: }
0872: out.append("\n");
0873:
0874: out.append("group_descriptions = new Array();")
0875: .append("\n");
0876: for (int i = 0; i < groupDescriptions.size(); i++) {
0877: out.append(
0878: " group_descriptions[" + i + "] = \""
0879: + groupDescriptions.get(i) + "\";")
0880: .append("\n");
0881: }
0882: out.append("\n");
0883:
0884: return out.toString();
0885: } catch (IOException e) {
0886: throw new ManagerException(e);
0887: } finally {
0888: lock.unlock();
0889: }
0890: }
0891:
0892: // private //////////////////////////////////////////////////////////////////////
0893: private synchronized ReentrantLock getLock(final File root) {
0894: if (locks.get(root) == null) {
0895: locks.put(root, new ReentrantLock());
0896: }
0897:
0898: return locks.get(root);
0899: }
0900:
0901: private File createBundleNoLock(final File root,
0902: final Platform platform, final String[] components,
0903: Properties props, Properties bundleProps)
0904: throws ManagerException {
0905: try {
0906: final String key = "" + platform.getCodeName() + ": "
0907: + StringUtils.asString(components);
0908:
0909: final File bundlesListFile = new File(root, BUNDLES_LIST);
0910:
0911: final List<String> bundlesList = FileUtils
0912: .readStringList(bundlesListFile);
0913: if (bundlesList.contains(key)) {
0914: return new File(bundlesList.get(bundlesList
0915: .indexOf(key) + 1));
0916: }
0917:
0918: final File registryXml = new File(root, REGISTRY_XML);
0919:
0920: final File tempDir = FileUtils.createTempFile(new File(
0921: root, TEMP), true, true);
0922: final File tempUserDir = FileUtils.createTempFile(tempDir,
0923: true, true);
0924: final File tempStatefile = FileUtils.createTempFile(
0925: tempDir, false);
0926:
0927: File bundle = new File(FileUtils.createTempFile(new File(
0928: root, BUNDLES), false), "bundle.jar");
0929:
0930: final Registry registry = new Registry();
0931: registry.setLocalDirectory(tempUserDir);
0932: registry.setFinishHandler(DummyFinishHandler.INSTANCE);
0933: registry.setTargetPlatform(platform);
0934: registry.loadProductRegistry(registryXml);
0935:
0936: List<Product> products = new LinkedList<Product>();
0937: for (String string : components) {
0938: final String[] parts = string.split(",");
0939: System.out.println(" processing: " + string);
0940:
0941: final Product product = registry.getProduct(parts[0],
0942: Version.getVersion(parts[1]));
0943:
0944: products.add(product);
0945: product.setStatus(Status.INSTALLED);
0946: }
0947: registry.saveStateFile(tempStatefile, new Progress());
0948:
0949: bundle.getParentFile().mkdirs();
0950:
0951: final File javaHome = new File(System
0952: .getProperty("java.home"));
0953:
0954: final File tempPropertiesFile = FileUtils.createTempFile(
0955: tempDir, false);
0956: OutputStream os = new FileOutputStream(tempPropertiesFile);
0957: props.store(os, null);
0958: os.close();
0959:
0960: final File tempBundlePropertiesFile = FileUtils
0961: .createTempFile(tempDir, false);
0962: os = new FileOutputStream(tempBundlePropertiesFile);
0963: bundleProps.store(os, null);
0964: os.close();
0965:
0966: final ExecutionResults results = SystemUtils
0967: .executeCommand(JavaUtils.getExecutable(javaHome)
0968: .getAbsolutePath(),
0969: "-Dnbi.product.remote.registries="
0970: + registryXml.toURI(), "-jar",
0971: getEngine(root).getAbsolutePath(),
0972: "--silent", "--state", tempStatefile
0973: .getAbsolutePath(),
0974: "--create-bundle",
0975: bundle.getAbsolutePath(), "--ignore-lock",
0976: "--platform", platform.toString(),
0977: "--userdir", tempUserDir.getAbsolutePath(),
0978: "--properties", tempPropertiesFile
0979: .getAbsolutePath(),
0980: "--bundle-properties",
0981: tempBundlePropertiesFile.getAbsolutePath());
0982:
0983: if (results.getErrorCode() != 0) {
0984: throw new ManagerException(
0985: "Could not create bundle - error in running the engine");
0986: }
0987:
0988: FileUtils.deleteFile(tempStatefile);
0989: FileUtils.deleteFile(tempPropertiesFile);
0990: FileUtils.deleteFile(tempUserDir, true);
0991:
0992: if (platform == Platform.WINDOWS) {
0993: bundle = new File(bundle.getAbsolutePath()
0994: .replaceFirst("\\.jar$", ".exe"));
0995: } else if (platform.isCompatibleWith(Platform.MACOSX)) {
0996: bundle = new File(bundle.getAbsolutePath()
0997: .replaceFirst("\\.jar$", ".zip"));
0998: } else {
0999: bundle = new File(bundle.getAbsolutePath()
1000: .replaceFirst("\\.jar$", ".sh"));
1001: }
1002:
1003: bundlesList.add(key);
1004: bundlesList.add(bundle.getAbsolutePath());
1005: FileUtils.writeStringList(bundlesListFile, bundlesList);
1006:
1007: return bundle;
1008: } catch (IOException e) {
1009: throw new ManagerException(e);
1010: } catch (InitializationException e) {
1011: throw new ManagerException(e);
1012: } catch (FinalizationException e) {
1013: throw new ManagerException(e);
1014: }
1015: }
1016:
1017: private void deleteBudlesNoLock(final File root)
1018: throws ManagerException {
1019: try {
1020: FileUtils.writeFile(new File(root, BUNDLES_LIST), "");
1021: FileUtils.deleteFile(new File(root, BUNDLES), true);
1022: FileUtils.mkdirs(new File(root, BUNDLES));
1023: } catch (IOException e) {
1024: throw new ManagerException(e);
1025: }
1026: }
1027:
1028: private void initializeRegistryNoLock(final File root)
1029: throws ManagerException {
1030: try {
1031: if (!root.exists()) {
1032: FileUtils.mkdirs(root);
1033: }
1034:
1035: final File temp = new File(root, TEMP);
1036: if (!temp.exists()) {
1037: FileUtils.mkdirs(temp);
1038: }
1039:
1040: Locale.setDefault(new Locale("en", "US"));
1041:
1042: DownloadManager.getInstance().setLocalDirectory(temp);
1043: DownloadManager.getInstance().setFinishHandler(
1044: DummyFinishHandler.INSTANCE);
1045:
1046: System.setProperty(Installer.LOCAL_DIRECTORY_PATH_PROPERTY,
1047: temp.getAbsolutePath());
1048: System.setProperty(Installer.IGNORE_LOCK_FILE_PROPERTY,
1049: "true");
1050: System.setProperty(LogManager.LOG_TO_CONSOLE_PROPERTY,
1051: "false");
1052: System.setProperty(Registry.LAZY_LOAD_ICONS_PROPERTY,
1053: "true");
1054:
1055: final File registryXml = new File(root, REGISTRY_XML);
1056: if (!registryXml.exists()) {
1057: new Registry().saveProductRegistry(registryXml,
1058: TrueFilter.INSTANCE, true, true, true);
1059: }
1060:
1061: final File bundlesList = new File(root, BUNDLES_LIST);
1062: if (!bundlesList.exists()) {
1063: FileUtils.writeFile(bundlesList, "");
1064: }
1065:
1066: final File bundles = new File(root, BUNDLES);
1067: if (!bundles.exists()) {
1068: FileUtils.mkdirs(bundles);
1069: }
1070:
1071: final File components = new File(root, COMPONENTS);
1072: if (!components.exists()) {
1073: FileUtils.mkdirs(components);
1074: }
1075: } catch (FinalizationException e) {
1076: throw new ManagerException(e);
1077: } catch (IOException e) {
1078: throw new ManagerException(e);
1079: }
1080: }
1081:
1082: private Registry loadRegistry(final File root,
1083: final File tempUserDir, final Platform platform)
1084: throws ManagerException {
1085: try {
1086: final File registryXml = new File(root, REGISTRY_XML);
1087:
1088: final Registry registry = new Registry();
1089: registry.setLocalDirectory(tempUserDir);
1090: registry.setFinishHandler(DummyFinishHandler.INSTANCE);
1091: registry.setTargetPlatform(platform);
1092: registry.loadProductRegistry(registryXml);
1093:
1094: return registry;
1095: } catch (InitializationException e) {
1096: throw new ManagerException(e);
1097: }
1098: }
1099:
1100: private void iterate(final Platform platform, final File root,
1101: final Registry registry, final Product[] combination,
1102: final int index, final List<Product> products,
1103: final int start) throws ManagerException {
1104: for (int i = start; i < products.size(); i++) {
1105: combination[index] = products.get(i);
1106:
1107: if (index == combination.length - 1) {
1108: for (Product product : products) {
1109: product.setStatus(Status.NOT_INSTALLED);
1110: }
1111: for (Product product : combination) {
1112: product.setStatus(Status.TO_BE_INSTALLED);
1113: }
1114:
1115: if (registry.getProductsToInstall().size() == combination.length) {
1116: String[] components = new String[combination.length];
1117:
1118: for (int j = 0; j < combination.length; j++) {
1119: components[j] = combination[j].getUid()
1120: + ","
1121: + combination[j].getVersion()
1122: .toString();
1123: }
1124:
1125: createBundle(root, platform, components);
1126: }
1127: } else {
1128: iterate(platform, root, registry, combination,
1129: index + 1, products, i + 1);
1130: }
1131: }
1132: }
1133:
1134: private List<Product> getProducts(RegistryNode root) {
1135: final List<Product> list = new LinkedList<Product>();
1136:
1137: for (RegistryNode node : root.getChildren()) {
1138: if (node instanceof Product) {
1139: list.add((Product) node);
1140: }
1141:
1142: list.addAll(getProducts(node));
1143: }
1144:
1145: return list;
1146: }
1147:
1148: private List<Group> getGroups(RegistryNode root) {
1149: final List<Group> list = new LinkedList<Group>();
1150:
1151: for (RegistryNode node : root.getChildren()) {
1152: if (node instanceof Group) {
1153: list.add((Group) node);
1154: }
1155:
1156: list.addAll(getGroups(node));
1157: }
1158:
1159: return list;
1160: }
1161: }
|