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-2007 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.projectopener;
043:
044: import java.io.BufferedInputStream;
045: import java.io.BufferedOutputStream;
046: import java.io.BufferedReader;
047: import java.io.File;
048: import java.io.FileFilter;
049: import java.io.FileNotFoundException;
050: import java.io.FileOutputStream;
051: import java.io.IOException;
052: import java.io.InputStream;
053: import java.io.OutputStream;
054: import java.io.PrintWriter;
055: import java.io.StringWriter;
056: import java.net.InetSocketAddress;
057: import java.net.MalformedURLException;
058: import java.net.Proxy;
059: import java.net.ProxySelector;
060: import java.net.SocketAddress;
061: import java.net.URI;
062: import java.net.URL;
063: import java.util.ArrayList;
064: import java.util.Collections;
065: import java.util.Enumeration;
066: import java.util.HashSet;
067: import java.util.List;
068: import java.util.Locale;
069: import java.util.Properties;
070: import java.util.Set;
071: import java.util.StringTokenizer;
072: import java.util.logging.Logger;
073: import java.util.regex.Matcher;
074: import java.util.regex.Pattern;
075: import java.util.zip.ZipEntry;
076: import java.util.zip.ZipFile;
077: import javax.jnlp.BasicService;
078: import javax.jnlp.FileContents;
079: import javax.jnlp.PersistenceService;
080: import javax.jnlp.ServiceManager;
081: import javax.jnlp.UnavailableServiceException;
082: import javax.swing.JOptionPane;
083: import javax.swing.UIManager;
084:
085: /**
086: *
087: * @author Milan Kubec
088: */
089: public class Utils {
090:
091: private static final int BUFF_SIZE = 2048;
092:
093: private static final String OS_NAME = System.getProperty("os.name")
094: .toLowerCase(Locale.US);
095:
096: private static Logger LOGGER = WSProjectOpener.LOGGER;
097:
098: private static final String winLauncher = "netbeans.exe";
099: private static final String unixLauncher = "netbeans";
100: private static final String macLauncher = "netbeans";
101:
102: private static final int SEARCH_DEPTH = 2;
103:
104: private static Properties properties;
105:
106: private static boolean savedProxyUsed = false;
107:
108: public static File anotherNBDir = null;
109:
110: Utils() {
111: }
112:
113: static {
114: try {
115: UIManager.setLookAndFeel(UIManager
116: .getSystemLookAndFeelClassName());
117: } catch (Exception ex) {
118: // nothing to do
119: }
120: }
121:
122: public static void unzip(File srcFile, File destDir)
123: throws IOException {
124: InputStream input = null;
125: OutputStream output = null;
126: ZipFile zipfile = new ZipFile(srcFile);
127: Enumeration zipEntries = zipfile.entries();
128: while (zipEntries.hasMoreElements()) {
129: ZipEntry entry = (ZipEntry) zipEntries.nextElement();
130: if (entry.isDirectory()) {
131: new File(destDir, entry.getName()).mkdir();
132: continue;
133: }
134: try {
135: input = new BufferedInputStream(zipfile
136: .getInputStream(entry));
137: File destFile = new File(destDir, entry.getName());
138: FileOutputStream fos = new FileOutputStream(destFile);
139: output = new BufferedOutputStream(fos);
140: copyStreams(input, output);
141: } finally {
142: if (input != null) {
143: input.close();
144: }
145: if (output != null) {
146: output.flush();
147: output.close();
148: }
149: }
150: }
151: }
152:
153: public static void download(String urlStr, File destFile)
154: throws IOException {
155: URL srcUrl = new URL(urlStr);
156: try {
157: List list = ProxySelector.getDefault().select(
158: srcUrl.toURI());
159: LOGGER.info("List of proxies from Proxy Selector: " + list);
160: } catch (Throwable t) {
161: // nothing happens, it's just for logging
162: }
163: InputStream input = null;
164: OutputStream output = null;
165: try {
166: input = srcUrl.openStream();
167: FileOutputStream fos = new FileOutputStream(destFile);
168: output = new BufferedOutputStream(fos);
169: copyStreams(input, output);
170: } finally {
171: if (input != null) {
172: input.close();
173: }
174: if (output != null) {
175: output.flush();
176: output.close();
177: }
178: }
179: }
180:
181: private static void installProxySelector(final String hostName,
182: final int portNum) {
183: ProxySelector.setDefault(new ProxySelector() {
184: public List select(URI uri) {
185: List list = new ArrayList();
186: list.add(new Proxy(Proxy.Type.HTTP,
187: new InetSocketAddress(hostName, portNum)));
188: return list;
189: }
190:
191: public void connectFailed(URI uri, SocketAddress sa,
192: IOException ioe) {
193: //
194: }
195: });
196: }
197:
198: private static void copyStreams(InputStream input,
199: OutputStream output) throws IOException {
200: int count;
201: byte data[] = new byte[BUFF_SIZE];
202: while ((count = input.read(data, 0, BUFF_SIZE)) != -1) {
203: output.write(data, 0, count);
204: }
205: }
206:
207: public static File createTempFile(File dir, String prefix,
208: String suffix, boolean delOnExit) throws IOException {
209: File tempFile = File.createTempFile(prefix, suffix, dir);
210: if (delOnExit) {
211: tempFile.deleteOnExit();
212: }
213: return tempFile;
214: }
215:
216: public static File createTempDir(File dir, String prefix)
217: throws IOException {
218: File tempDir = File.createTempFile(prefix, "", dir);
219: if (!tempDir.delete()) {
220: throw new IOException("Cannot delete file: "
221: + tempDir.getAbsolutePath());
222: }
223: if (!tempDir.mkdir()) {
224: throw new IOException("Cannot create folder: "
225: + tempDir.getAbsolutePath());
226: }
227: return tempDir;
228: }
229:
230: public static String getPlatformLauncher() {
231: String retVal = "";
232: if (OS_NAME.indexOf("win") != -1) {
233: retVal = winLauncher;
234: } else if (OS_NAME.indexOf("unix") != -1) {
235: retVal = unixLauncher;
236: } else if (OS_NAME.indexOf("linux") != -1) {
237: retVal = unixLauncher;
238: } else if (OS_NAME.indexOf("mac os") != -1) {
239: retVal = macLauncher;
240: } else if (OS_NAME.indexOf("solaris") != -1) {
241: retVal = unixLauncher;
242: } else if (OS_NAME.indexOf("sunos") != -1) {
243: retVal = unixLauncher;
244: }
245: return retVal;
246: }
247:
248: /**
249: *
250: */
251: public static String exc2String(Throwable t) {
252: StringWriter writer = new StringWriter();
253: t.printStackTrace(new PrintWriter(writer));
254: writer.flush();
255: return writer.toString();
256: }
257:
258: /**
259: *
260: */
261: public static SavedProjects getSavedProjects(File dir) {
262: Set projectTypes = new HashSet();
263: List savedProjects = savedNbProjects(dir, 0, projectTypes);
264: return new SavedProjects(savedProjects, projectTypes);
265: }
266:
267: // searches also recursively to find nested projects
268: private static List savedNbProjects(File dir, int depth, Set pTypes) {
269: if (depth > SEARCH_DEPTH) {
270: return Collections.EMPTY_LIST;
271: }
272: List sProjects = new ArrayList();
273: File subdirs[] = dir.listFiles(new FileFilter() {
274: public boolean accept(File f) {
275: if (f.isDirectory()) {
276: return true;
277: }
278: return false;
279: }
280: });
281: for (int i = 0; i < subdirs.length; i++) {
282: ProjectType pt = getNbProjectType(subdirs[i]);
283: if (pt != null) {
284: SavedProjects.OneProject sp = new SavedProjects.OneProject(
285: subdirs[i]);
286: sProjects.add(sp);
287: pTypes.add(pt);
288: }
289: sProjects.addAll(savedNbProjects(subdirs[i], depth + 1,
290: pTypes));
291: }
292: return sProjects;
293: }
294:
295: private static ProjectType getNbProjectType(File f) {
296: assert f != null;
297: File prjXmlFile = new File(new File(f, "nbproject"),
298: "project.xml");
299: if (prjXmlFile.exists() && prjXmlFile.isFile()) {
300: switch (getProjectType(f)) {
301: case ProjectType.J2SE_TYPE:
302: return ProjectType.J2SE;
303: case ProjectType.FREEFORM_TYPE:
304: return ProjectType.FREEFORM;
305: case ProjectType.J2ME_TYPE:
306: return ProjectType.J2ME;
307: case ProjectType.WEB_TYPE:
308: return ProjectType.WEB;
309: case ProjectType.EJB_TYPE:
310: return ProjectType.EJB;
311: case ProjectType.EAR_TYPE:
312: return ProjectType.EAR;
313: }
314: }
315: // try other unusual projects:
316: // 1) Maven
317: File pomFile = new File(f, "pom.xml");
318: if (pomFile.exists()) {
319: return ProjectType.MAVEN;
320: }
321: return null;
322: }
323:
324: private static int getProjectType(File f) {
325: assert f != null;
326: File prjXmlFile = new File(new File(f, "nbproject"),
327: "project.xml");
328: if (prjXmlFile.exists() && prjXmlFile.isFile()) {
329: BufferedReader reader = null;
330: try {
331: reader = new java.io.BufferedReader(
332: new java.io.FileReader(prjXmlFile));
333: String line = reader.readLine();
334: while (line != null) {
335: if (line.indexOf("<type>") != -1) {
336: return getTypeFromLine(line);
337: }
338: line = reader.readLine();
339: }
340: } catch (FileNotFoundException ex) {
341: ex.printStackTrace();
342: } catch (IOException ioe) {
343: ioe.printStackTrace();
344: } finally {
345: try {
346: reader.close();
347: } catch (IOException ex) {
348: ex.printStackTrace();
349: }
350: }
351: }
352: return ProjectType.UNKNOWN_TYPE;
353: }
354:
355: private static int getTypeFromLine(String s) {
356: if (s.indexOf("<type>" + ProjectType.J2SE_NAME + "</type>") != -1) {
357: return ProjectType.J2SE_TYPE;
358: } else if (s.indexOf("<type>" + ProjectType.FREEFORM_NAME
359: + "</type>") != -1) {
360: return ProjectType.FREEFORM_TYPE;
361: } else if (s.indexOf("<type>" + ProjectType.J2ME_NAME
362: + "</type>") != -1) {
363: return ProjectType.J2ME_TYPE;
364: } else if (s.indexOf("<type>" + ProjectType.WEB_NAME
365: + "</type>") != -1) {
366: return ProjectType.WEB_TYPE;
367: } else if (s.indexOf("<type>" + ProjectType.EJB_NAME
368: + "</type>") != -1) {
369: return ProjectType.EJB_TYPE;
370: } else if (s.indexOf("<type>" + ProjectType.EAR_NAME
371: + "</type>") != -1) {
372: return ProjectType.EAR_TYPE;
373: }
374: return ProjectType.UNKNOWN_TYPE;
375: }
376:
377: public static void showErrMessage(String message, String title) {
378: JOptionPane.showMessageDialog(null, message, title,
379: JOptionPane.ERROR_MESSAGE);
380: }
381:
382: public static boolean maybeAnotherProxy() {
383:
384: if (!savedProxyUsed) {
385: // get saved properties from PersistenceService
386: String proxyHost = getProperty("jws.http.proxyHost");
387: String proxyPort = getProperty("jws.http.proxyPort");
388:
389: // if there are some valid proxy settings saved already try them
390: if (proxyHost != null && !proxyHost.equals("")
391: && proxyPort != null && !proxyPort.equals("")) {
392: int portNum = Integer.parseInt(proxyPort);
393: LOGGER.info("Using saved proxy settings: proxyHost = "
394: + proxyHost + ", proxyPort = " + proxyPort);
395: installProxySelector(proxyHost, portNum);
396: savedProxyUsed = true;
397: return true;
398: }
399: }
400:
401: // ask user for better proxy settigns
402: ProxySettings ps = new ProxySettings();
403: DialogDescriptor dd = new DialogDescriptor();
404: ProxySettingsDialog pd = new ProxySettingsDialog(ps, dd);
405: pd.setVisible(true);
406: pd.dispose();
407:
408: if (dd.getValue().equals(DialogDescriptor.CONTINUE)) {
409: // try to install own ProxySelector
410: String portNumStr = ps.getProxyPort();
411: if (!portNumStr.equals("") && portNumStr != null) {
412: String hostName = ps.getProxyHost();
413: int portNum = Integer.parseInt(ps.getProxyPort());
414: installProxySelector(hostName, portNum);
415: // store proxy settings using PersistentService
416: setProperty("jws.http.proxyHost", hostName);
417: setProperty("jws.http.proxyPort", portNumStr);
418: }
419: return true;
420: } else {
421: return false;
422: }
423:
424: }
425:
426: public static Integer getAnotherNBInstallDir(String nbv) {
427: DialogDescriptor dd = new DialogDescriptor();
428: NBInstallDir nbd = new NBInstallDir();
429: BrowseNetBeansDialog browseNBDialog = new BrowseNetBeansDialog(
430: dd, nbd, nbv);
431: browseNBDialog.setVisible(true);
432: browseNBDialog.dispose();
433: if (dd.getValue() != null) {
434: if (dd.getValue().equals(DialogDescriptor.CONTINUE)) {
435: anotherNBDir = nbd.getInstallDir();
436: return DialogDescriptor.CONTINUE;
437: } else if (dd.getValue().equals(DialogDescriptor.DOWNLOAD)) {
438: return DialogDescriptor.DOWNLOAD;
439: }
440: }
441: return DialogDescriptor.EXIT;
442: }
443:
444: // ---
445:
446: public static BasicService getBasicService() {
447: BasicService service = null;
448: try {
449: service = (BasicService) ServiceManager
450: .lookup("javax.jnlp.BasicService");
451: } catch (UnavailableServiceException ex) {
452: // no service => return null
453: }
454: return service;
455: }
456:
457: public static PersistenceService getPersistenceService() {
458: PersistenceService service = null;
459: try {
460: service = (PersistenceService) ServiceManager
461: .lookup("javax.jnlp.PersistenceService");
462: } catch (UnavailableServiceException ex) {
463: // no service => return null
464: }
465: return service;
466: }
467:
468: // ---
469:
470: public static String getProperty(String pName) {
471: if (properties == null) {
472: loadProperties();
473: }
474: String pVal = properties.getProperty(pName);
475: LOGGER.info("Getting property: " + pName + " = " + pVal);
476: return pVal;
477: }
478:
479: public static void setProperty(String pName, String pVal) {
480: LOGGER.info("Setting property: " + pName + " = " + pVal);
481: if (properties == null) {
482: loadProperties();
483: }
484: properties.setProperty(pName, pVal);
485: saveProperties();
486: }
487:
488: private static void loadProperties() {
489: BasicService bService = getBasicService();
490: PersistenceService pService = getPersistenceService();
491: properties = new Properties();
492: try {
493: FileContents fc = pService.get(bService.getCodeBase());
494: properties.load(fc.getInputStream());
495: } catch (Exception ex) {
496: // no props will be loaded
497: LOGGER.info(Utils.exc2String(ex));
498: }
499: }
500:
501: private static void saveProperties() {
502: BasicService bService = getBasicService();
503: PersistenceService pService = getPersistenceService();
504: URL codeBase = bService.getCodeBase();
505: FileContents fc = null;
506: try {
507: fc = pService.get(codeBase);
508: } catch (FileNotFoundException fnfe) {
509: try {
510: // Entry probably doesn't exist => create it
511: // XXX the size is arbitrary
512: pService.create(codeBase, 30000L);
513: fc = pService.get(codeBase);
514: } catch (Exception ex) {
515: LOGGER.info(Utils.exc2String(ex));
516: }
517: } catch (Exception ex) {
518: // no props will be saved
519: LOGGER.info(Utils.exc2String(ex));
520: }
521: try {
522: properties.store(fc.getOutputStream(true), null);
523: } catch (IOException ioe) {
524: LOGGER.info(Utils.exc2String(ioe));
525: }
526: }
527:
528: // ---
529:
530: /**
531: * Tries to open passed URL in system browser
532: * using JNLP BasicService
533: */
534: public static boolean showDocument(String url) {
535: URL url2Show = null;
536: try {
537: url2Show = new java.net.URL(url);
538: } catch (MalformedURLException ex) {
539: // nothing much to do here
540: }
541: BasicService service = getBasicService();
542: if (service != null) {
543: return service.showDocument(url2Show);
544: }
545: return false;
546: }
547:
548: // ---
549:
550: /**
551: * Returns parts of the NB version number if it matches the regexp
552: * e.g. '1.2.3beta2' = > [0] == 1.2.3, [1] == beta, [2] == 2
553: * if not matches returns null
554: */
555: public static String[] getVersionParts(String s) {
556: if (s == null) {
557: return null;
558: }
559: String retVal[] = new String[] { "", "", "" };
560: Pattern p = Pattern
561: .compile("(\\d*(\\.\\d+)*)([a-zA-Z]*)(\\d*)");
562: Matcher m = p.matcher(s);
563: if (m.matches()) {
564: retVal[0] = m.group(1);
565: retVal[1] = m.group(3);
566: retVal[2] = m.group(4);
567: return retVal;
568: }
569: return null;
570: }
571:
572: /**
573: * Compares two NB versions (only numbers), e.g. 5.5.1, 6.0,
574: * returns negative if first version number parameter is lower than second,
575: * positive if first is higher and 0 if both versions are the same
576: */
577: public static int compareVersions(String verStr1, String verStr2) {
578: int vd1[] = parseVersionString(verStr1);
579: int vd2[] = parseVersionString(verStr2);
580: int len1 = vd1.length;
581: int len2 = vd2.length;
582: int max = Math.max(len1, len2);
583: for (int i = 0; i < max; i++) {
584: int d1 = ((i < len1) ? vd1[i] : 0);
585: int d2 = ((i < len2) ? vd2[i] : 0);
586: if (d1 != d2) {
587: return d1 - d2;
588: }
589: }
590: return 0;
591: }
592:
593: /**
594: * Compare release types in following way: dev < beta < rc < ""
595: */
596: public static int compareReleaseTypes(String relType1,
597: String relType2) {
598: int retVal = 0;
599: if (relType1.equals(relType2)) {
600: retVal = 0;
601: } else if (relType1.equals("")) {
602: retVal = 1;
603: } else if (relType2.equals("")) {
604: retVal = -1;
605: } else if (relType1.equals("dev")
606: && ((relType2.equals("beta") || relType2.equals("rc")))) {
607: retVal = -1;
608: } else if (relType2.equals("dev")
609: && ((relType1.equals("beta") || relType1.equals("rc")))) {
610: retVal = 1;
611: } else if (relType1.equals("beta") && relType2.equals("rc")) {
612: retVal = -1;
613: } else if (relType2.equals("beta") && relType1.equals("rc")) {
614: retVal = 1;
615: }
616: return retVal;
617: }
618:
619: private static int[] parseVersionString(String s)
620: throws NumberFormatException {
621: StringTokenizer st = new StringTokenizer(s, ".", true);
622: int len = st.countTokens();
623: if ((len % 2) == 0) {
624: throw new NumberFormatException(
625: "Even number of pieces in a spec version: '" + s
626: + "'"); // NOI18N
627: }
628: int i = 0;
629: int[] digits = new int[len / 2 + 1];
630: boolean expectingNumber = true;
631: while (st.hasMoreTokens()) {
632: if (expectingNumber) {
633: expectingNumber = false;
634: int piece = Integer.parseInt(st.nextToken());
635: if (piece < 0) {
636: throw new NumberFormatException(
637: "Spec version component < 0: " + piece); // NOI18N
638: }
639: digits[i++] = piece;
640: } else {
641: if (!".".equals(st.nextToken())) { // NOI18N
642: throw new NumberFormatException(
643: "Expected dot in spec version: '" + s + "'"); // NOI18N
644: }
645: expectingNumber = true;
646: }
647: }
648: return digits;
649: }
650:
651: // ---
652:
653: public static class ProxySettings {
654:
655: private String proxyHost;
656: private String proxyPort;
657:
658: public ProxySettings() {
659: proxyHost = "";
660: proxyPort = "";
661: }
662:
663: public ProxySettings(String host, String port) {
664: proxyHost = host;
665: proxyPort = port;
666: }
667:
668: public String getProxyHost() {
669: return proxyHost;
670: }
671:
672: public String getProxyPort() {
673: return proxyPort;
674: }
675:
676: public void setProxyHost(String s) {
677: proxyHost = s;
678: }
679:
680: public void setProxyPort(String s) {
681: proxyPort = s;
682: }
683:
684: }
685:
686: public static class DialogDescriptor {
687:
688: public static final Integer EXIT = new Integer(0);
689: public static final Integer CONTINUE = new Integer(1);
690: public static final Integer DOWNLOAD = new Integer(2);
691:
692: private Object value;
693:
694: public DialogDescriptor() {
695: }
696:
697: public Object getValue() {
698: return value;
699: }
700:
701: public void setValue(Object val) {
702: value = val;
703: }
704:
705: }
706:
707: public static class NBInstallDir {
708: private File dir;
709:
710: public NBInstallDir() {
711: }
712:
713: public File getInstallDir() {
714: return dir;
715: }
716:
717: public void setInstallDir(File d) {
718: dir = d;
719: }
720: }
721:
722: }
|