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
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.identity.samples.util;
043:
044: import java.io.*;
045: import java.util.Enumeration;
046: import java.util.Properties;
047: import java.util.zip.ZipEntry;
048: import java.util.zip.ZipInputStream;
049: import org.netbeans.api.project.Project;
050: import org.netbeans.api.project.ProjectManager;
051: import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties;
052: import org.netbeans.spi.project.support.ant.AntProjectHelper;
053: import org.netbeans.spi.project.support.ant.EditableProperties;
054: import org.openide.ErrorManager;
055: import org.openide.filesystems.FileLock;
056: import org.openide.filesystems.FileObject;
057: import org.openide.filesystems.FileUtil;
058: import org.openide.xml.XMLUtil;
059: import org.w3c.dom.Document;
060: import org.w3c.dom.Element;
061: import org.w3c.dom.Node;
062: import org.w3c.dom.NodeList;
063: import org.w3c.dom.Text;
064: import org.xml.sax.InputSource;
065:
066: public class SoaSampleUtils {
067:
068: public static String[] xlateFiles = { "build-impl.xml", // NOI18N
069: "project.xml", // NOI18N
070: "project.properties", // NOI18N
071: "AssemblyInformation.xml" // NOI18N
072: };
073:
074: public static void unZipFile(InputStream source,
075: FileObject rootFolder) throws IOException {
076: try {
077: ZipInputStream str = new ZipInputStream(source);
078: ZipEntry entry;
079: while ((entry = str.getNextEntry()) != null) {
080: if (entry.isDirectory()) {
081: continue;
082: }
083: FileObject fo = FileUtil.createData(rootFolder, entry
084: .getName());
085: FileLock lock = fo.lock();
086: try {
087: OutputStream out = fo.getOutputStream(lock);
088: try {
089: FileUtil.copy(str, out);
090: } finally {
091: out.close();
092: }
093: } finally {
094: lock.releaseLock();
095: }
096: }
097: } finally {
098: source.close();
099: }
100: }
101:
102: public static void unZipFileTranslateProjectName(
103: InputStream source, FileObject rootFolder, String name,
104: String token) throws IOException {
105: try {
106: ZipInputStream str = new ZipInputStream(source);
107: ZipEntry entry;
108: while ((entry = str.getNextEntry()) != null) {
109: if (entry.isDirectory()) {
110: continue;
111: }
112: String fname = entry.getName();
113: FileObject fo = FileUtil.createData(rootFolder, fname);
114: FileLock lock = fo.lock();
115: try {
116: OutputStream out = fo.getOutputStream(lock);
117: try {
118: if (needTranslation(fname)) {
119: translateProjectName(str, out, name, token);
120: } else {
121: FileUtil.copy(str, out);
122: }
123: } finally {
124: out.close();
125: }
126: } finally {
127: lock.releaseLock();
128: }
129: }
130: } finally {
131: source.close();
132: }
133: }
134:
135: static boolean needTranslation(String fname) {
136: for (int i = 0; i < xlateFiles.length; i++) {
137: if (fname.endsWith(xlateFiles[i])) {
138: return true;
139: }
140: }
141: return false;
142: }
143:
144: static void translateProjectName(InputStream str, OutputStream out,
145: String name, String token) throws IOException {
146: ByteArrayOutputStream bo = new ByteArrayOutputStream();
147: FileUtil.copy(str, bo);
148: ByteArrayInputStream bi = new ByteArrayInputStream(bo
149: .toString().replaceAll(token, name).getBytes());
150: FileUtil.copy(bi, out);
151: }
152:
153: public static FileObject getStudioUserDir() {
154: Log.out("StudioUserDir: "
155: + FileUtil.toFileObject(
156: new File(System.getProperty("netbeans.user")))
157: .getPath()); // NOI18N
158: return FileUtil.toFileObject(new File(System
159: .getProperty("netbeans.user"))); // NOI18N
160: }
161:
162: public static FileObject getProjectFolder(FileObject parentDir,
163: String projectDirName) {
164: assert parentDir != null : parentDir + "/" + projectDirName
165: + "doesn't exist"; // NOI18N
166: assert projectDirName != null : "project name can't be empty"; // NOI18N
167: return parentDir.getFileObject(projectDirName);
168: }
169:
170: private static FileObject getFolder(String relative) {
171: return getFolder(getStudioUserDir(), relative);
172: }
173:
174: private static FileObject getFolder(FileObject parent,
175: String relative) {
176: FileObject folder = parent.getFileObject(relative);
177: if (folder != null) {
178: return folder;
179: }
180: try {
181: folder = parent.createFolder(relative);
182: } catch (IOException ioe) {
183: ErrorManager.getDefault().notify(ErrorManager.EXCEPTION,
184: ioe);
185: }
186: return folder;
187: }
188:
189: private static String getPropertiesPath(String name) {
190: return SoaSampleProjectProperties.getDefault()
191: .isPrivateProperty(name) ? AntProjectHelper.PRIVATE_PROPERTIES_PATH
192: : AntProjectHelper.PROJECT_PROPERTIES_PATH;
193: }
194:
195: private static AntProjectHelper getAntProjectHelper(Project project) {
196: return (AntProjectHelper) project.getLookup().lookup(
197: AntProjectHelper.class);
198: }
199:
200: public static void setPrivateProperty(FileObject prjLoc,
201: String name, String value) {
202: Properties properties = new Properties();
203: try {
204: properties.setProperty(name, value);
205:
206: FileObject propFile = FileUtil.createData(prjLoc,
207: AntProjectHelper.PRIVATE_PROPERTIES_PATH);
208: FileLock lock = propFile.lock();
209: OutputStream os = propFile.getOutputStream(lock);
210: try {
211: properties.store(os, null);
212: } finally {
213: os.close();
214: lock.releaseLock();
215: }
216: } catch (IOException e) {
217: e.printStackTrace();
218: }
219: }
220:
221: public static String getProperty(Project project, String name) {
222: return getProperties(project, name).getProperty(name);
223: }
224:
225: private static EditableProperties getProperties(Project project,
226: String name) {
227: AntProjectHelper helper = getAntProjectHelper(project);
228: assert helper != null : "Can't get AntProjectHelper for project: " // NOI18N
229: + project;
230: return helper.getProperties(getPropertiesPath(name));
231: }
232:
233: /**
234: * @return SunApp default server instance location
235: */
236: public static String getDefaultSunAppLocation() {
237: String loc = null;
238: String[] instances = InstanceProperties.getInstanceList();
239: for (String instanceId : instances) {
240: if (instanceId
241: .indexOf(SoaSampleProjectProperties.SERVER_INSTANCE_SUN_APPSERVER) != -1) {
242: int endIdx = instanceId.indexOf(']');
243: loc = instanceId.substring(1, endIdx);
244: break;
245: }
246:
247: }
248: return loc;
249: }
250:
251: /**
252: * Method taken from NB anagram code.
253: */
254: public static void setProjectName(FileObject prjLoc,
255: String projTypeName, String name) {
256: try {
257: // update project.xml
258: File projXml = FileUtil.toFile(prjLoc
259: .getFileObject(AntProjectHelper.PROJECT_XML_PATH));
260: Document doc = XMLUtil.parse(new InputSource(projXml
261: .toURI().toString()), false, true, null, null);
262: NodeList nlist = doc.getElementsByTagNameNS(projTypeName,
263: "name"); //NOI18N
264: if (nlist != null) {
265: for (int i = 0; i < nlist.getLength(); i++) {
266: Node n = nlist.item(i);
267: if (n.getNodeType() != Node.ELEMENT_NODE) {
268: continue;
269: }
270: Element e = (Element) n;
271:
272: replaceText(e, name);
273: }
274: saveXml(doc, prjLoc, AntProjectHelper.PROJECT_XML_PATH);
275: }
276:
277: } catch (Exception e) {
278: ErrorManager.getDefault().notify(e);
279: }
280:
281: }
282:
283: /**
284: * Method taken from NB anagram game.
285: * Extract nested text from an element.
286: * Currently does not handle coalescing text nodes, CDATA sections, etc.
287: * @param parent a parent element
288: */
289: private static void replaceText(Element parent, String name) {
290: NodeList l = parent.getChildNodes();
291: for (int i = 0; i < l.getLength(); i++) {
292: if (l.item(i).getNodeType() == Node.TEXT_NODE) {
293: Text text = (Text) l.item(i);
294: text.setNodeValue(name);
295: return;
296: }
297: }
298: }
299:
300: /**
301: * Method taken from NB anagram game.
302: * Save an XML config file to a named path.
303: * If the file does not yet exist, it is created.
304: */
305: private static void saveXml(Document doc, FileObject dir,
306: String path) throws IOException {
307: FileObject xml = FileUtil.createData(dir, path);
308: FileLock lock = xml.lock();
309: try {
310: OutputStream os = xml.getOutputStream(lock);
311: try {
312: XMLUtil.write(doc, os, "UTF-8"); // NOI18N
313: } finally {
314: os.close();
315: }
316: } finally {
317: lock.releaseLock();
318: }
319: }
320:
321: public static void insertParameters(FileObject dir,
322: String bpelProjDir) {
323: Enumeration files = dir.getData(true);
324:
325: while (files.hasMoreElements()) {
326: FileObject fileObject = (FileObject) files.nextElement();
327:
328: if (fileObject.isFolder())
329: continue;
330:
331: if (!((fileObject.getExt().toLowerCase().equals("xml") || // NOI18N
332: fileObject.getExt().toLowerCase().equals("properties")))) // NOI18N
333: continue;
334:
335: String line;
336: StringBuffer buffer = new StringBuffer();
337:
338: try {
339: InputStream inputStream = fileObject.getInputStream();
340: BufferedReader reader = new BufferedReader(
341: new InputStreamReader(inputStream));
342:
343: while ((line = reader.readLine()) != null) {
344: line = line.replace("__BPELPROJECTNAME__",
345: bpelProjDir); // NOI18N
346: buffer.append(line);
347: buffer.append("\n"); // NOI18N
348: }
349:
350: File file = FileUtil.toFile(fileObject);
351: OutputStream outputStream = new FileOutputStream(file);
352: PrintWriter writer = new PrintWriter(outputStream);
353: writer.write(buffer.toString());
354: writer.flush();
355: outputStream.close();
356: } catch (IOException ex) {
357: ErrorManager.getDefault().notify(
358: ErrorManager.INFORMATIONAL, ex);
359: }
360: }
361: }
362:
363: }
|