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.build.ant.registries;
038:
039: import java.io.File;
040: import java.io.FileInputStream;
041: import java.io.IOException;
042: import java.io.InputStream;
043: import java.net.URL;
044: import java.util.LinkedList;
045: import java.util.List;
046: import java.util.Properties;
047: import java.util.Vector;
048: import org.apache.tools.ant.BuildException;
049: import org.apache.tools.ant.Task;
050: import org.apache.tools.ant.taskdefs.Property;
051: import org.netbeans.installer.infra.build.ant.utils.Utils;
052: import org.netbeans.installer.utils.StringUtils;
053: import org.netbeans.installer.utils.exceptions.ParseException;
054: import org.netbeans.installer.utils.helper.Platform;
055: import org.netbeans.installer.infra.lib.registries.ManagerException;
056: import org.netbeans.installer.infra.lib.registries.impl.RegistriesManagerImpl;
057:
058: /**
059: *
060: * @author ks152834
061: */
062: public class CreateBundle extends Task {
063: private List<Component> componentObjects = new LinkedList<Component>();
064:
065: private File root;
066: private File target;
067: private Platform platform;
068: private Vector<Property> properties = new Vector<Property>();
069: private Vector<BundleProperty> bundleProperties = new Vector<BundleProperty>();
070:
071: public void setRoot(final File root) {
072: this .root = root;
073: }
074:
075: public void setPlatform(final String platform) {
076: try {
077: this .platform = StringUtils.parsePlatform(platform);
078: } catch (ParseException e) {
079: log(e.getMessage());
080: }
081: }
082:
083: public void setTarget(final File target) {
084: this .target = target;
085: }
086:
087: public Component createComponent() {
088: final Component component = new Component();
089:
090: componentObjects.add(component);
091: return component;
092: }
093:
094: public void addProperty(Property p) {
095: properties.addElement(p);
096: }
097:
098: public void addBundleProperty(BundleProperty p) {
099: bundleProperties.addElement(p);
100: }
101:
102: @Override
103: public void execute() throws BuildException {
104: try {
105: final List<String> components = new LinkedList<String>();
106: for (Component component : componentObjects) {
107: components.add(component.getUid() + ","
108: + component.getVersion());
109: }
110:
111: System.out.println("Creating bundle: " + platform + ": "
112: + components);
113: Properties props = readProperties(properties);
114: Properties bundleprops = readProperties(bundleProperties);
115:
116: final File bundle = new RegistriesManagerImpl()
117: .createBundle(root, platform, components
118: .toArray(new String[components.size()]),
119: props, bundleprops);
120:
121: Utils.copy(bundle, target);
122: } catch (ManagerException e) {
123: throw new BuildException(e);
124: } catch (IOException e) {
125: throw new BuildException(e);
126: }
127: }
128:
129: private Properties readProperties(
130: Vector<? extends Property> antProperties)
131: throws IOException {
132: Properties props = new Properties();
133: for (Property prop : antProperties) {
134: if (prop.getName() != null) {
135: if (prop.getValue() != null) {
136: props.setProperty(prop.getName(), prop.getValue());
137: } else if (prop.getLocation() != null) {
138: props.setProperty(prop.getName(), new File(prop
139: .getLocation().getFileName())
140: .getAbsolutePath());
141: }
142: } else if (prop.getFile() != null || prop.getUrl() != null) {
143: InputStream is = null;
144: try {
145: is = (prop.getFile() != null) ? new FileInputStream(
146: prop.getFile())
147: : prop.getUrl().openStream();
148:
149: Properties loadedProps = new Properties();
150: loadedProps.load(is);
151: is.close();
152: if (prop.getPrefix() != null) {
153: for (Object p : loadedProps.keySet()) {
154: props.setProperty(prop.getPrefix() + p,
155: loadedProps.getProperty(p
156: .toString()));
157: }
158: } else {
159: props.putAll(loadedProps);
160: }
161: } finally {
162: if (is != null) {
163: is.close();
164: }
165: }
166: }
167: }
168:
169: return props;
170: }
171:
172: public static class Component {
173: private String uid;
174: private String version;
175:
176: public void setUid(final String uid) {
177: this .uid = uid;
178: }
179:
180: public String getUid() {
181: return uid;
182: }
183:
184: public void setVersion(final String version) {
185: this .version = version;
186: }
187:
188: public String getVersion() {
189: return version;
190: }
191: }
192:
193: public static class BundleProperty extends Property {
194:
195: }
196: }
|