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: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.modules.profiler.attach.providers;
042:
043: import org.netbeans.api.java.platform.JavaPlatform;
044: import org.netbeans.api.java.platform.JavaPlatformManager;
045: import org.netbeans.api.java.platform.Specification;
046: import org.openide.filesystems.FileObject;
047: import org.openide.filesystems.FileStateInvalidException;
048: import org.openide.modules.SpecificationVersion;
049: import java.io.UnsupportedEncodingException;
050: import java.net.URLDecoder;
051: import java.util.Iterator;
052: import java.util.LinkedList;
053: import java.util.List;
054: import java.util.Vector;
055:
056: /**
057: *
058: * @author Jaroslav Bachorik
059: */
060: public class TargetPlatform {
061: //~ Inner Interfaces ---------------------------------------------------------------------------------------------------------
062:
063: public interface TargetPlatformFilter {
064: //~ Methods --------------------------------------------------------------------------------------------------------------
065:
066: public boolean isSupported(TargetPlatform javaPlatform);
067: }
068:
069: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
070:
071: private static List supportedPlatforms = null;
072:
073: //~ Instance fields ----------------------------------------------------------------------------------------------------------
074:
075: private SpecificationVersion version;
076: private String displayName;
077: private String javaHome;
078: private boolean defaultFlag;
079: private boolean validFlag;
080:
081: //~ Constructors -------------------------------------------------------------------------------------------------------------
082:
083: public TargetPlatform(final JavaPlatform platform) {
084: this (platform, false);
085: }
086:
087: public TargetPlatform(final JavaPlatform platform,
088: final boolean setDefault) {
089: this .defaultFlag = setDefault;
090:
091: try {
092: this .displayName = platform.getDisplayName();
093: this .version = platform.getSpecification().getVersion();
094:
095: FileObject folder = (FileObject) platform
096: .getInstallFolders().iterator().next();
097: final String hostOS = System.getProperty("os.name"); // NOI18N
098: this .javaHome = URLDecoder.decode(
099: folder.getURL().getPath(), "utf8"); // NOI18N
100:
101: if (this .javaHome.endsWith("/")) { // NOI18N
102: this .javaHome = this .javaHome.substring(0,
103: this .javaHome.length() - 1);
104: }
105:
106: if (hostOS.contains("Windows")
107: && this .javaHome.startsWith("/")) { // NOI18N
108: this .javaHome = this .javaHome.substring(1).replace('/',
109: '\\'); // NOI18N
110: }
111:
112: validFlag = true;
113: } catch (FileStateInvalidException ex) {
114: validFlag = false;
115: } catch (UnsupportedEncodingException e) {
116: validFlag = false;
117: } catch (Exception e) {
118: validFlag = false;
119: }
120: }
121:
122: //~ Methods ------------------------------------------------------------------------------------------------------------------
123:
124: public static final List getPlatformList(boolean caching) {
125: List platforms = getSupportedPlatforms(caching);
126:
127: return platforms;
128: }
129:
130: public static final List getPlatformList(
131: TargetPlatformFilter filter, boolean caching) {
132: final List platformList = getSupportedPlatforms(caching);
133: List newList = new Vector(); // IMPORTANT - at least as long as using DefaultComboBoxModel in JavaPlatformPanelComponent
134:
135: for (Iterator it = platformList.iterator(); it.hasNext();) {
136: TargetPlatform platform = (TargetPlatform) it.next();
137:
138: if (filter.isSupported(platform)) {
139: newList.add(platform);
140: }
141: }
142:
143: return newList;
144: }
145:
146: public static void refresh() {
147: getSupportedPlatforms(false);
148: }
149:
150: public TargetPlatformEnum getAsEnum() {
151: if (this .version.toString().startsWith("1.5")
152: || this .version.toString().startsWith("5")) { // NOI18N
153:
154: return TargetPlatformEnum.JDK5;
155: }
156:
157: if (this .version.toString().startsWith("1.6")
158: || this .version.toString().startsWith("6")) { // NOI18N
159:
160: return TargetPlatformEnum.JDK6;
161: }
162:
163: if (this .version.toString().startsWith("1.7")
164: || this .version.toString().startsWith("7")) { // NOI18N
165:
166: return TargetPlatformEnum.JDK7;
167: }
168:
169: return TargetPlatformEnum.JDK5;
170: }
171:
172: public boolean isDefault() {
173: return defaultFlag;
174: }
175:
176: public String getHomePath() {
177: return this .javaHome;
178: }
179:
180: public String getName() {
181: return getAsEnum().toString();
182: }
183:
184: public boolean isValid() {
185: return validFlag;
186: }
187:
188: public boolean equals(Object otherPlatform) {
189: if ((this .getHomePath() == null) || (otherPlatform == null)) {
190: return false; // fail early
191: }
192:
193: if (otherPlatform instanceof String) {
194: return this .getHomePath().equals(otherPlatform)
195: || this .toString().equals(otherPlatform);
196: }
197:
198: if (!(otherPlatform instanceof TargetPlatform)) {
199: return false;
200: }
201:
202: return (this .getHomePath()
203: .equals(((TargetPlatform) otherPlatform).getHomePath()));
204: }
205:
206: public int hashCode() {
207: return this .getHomePath().hashCode();
208: }
209:
210: public String toString() {
211: return this .displayName;
212: }
213:
214: private static List getSupportedPlatforms(final boolean cached) {
215: if ((supportedPlatforms == null) || !cached) {
216: supportedPlatforms = new LinkedList();
217:
218: try {
219: JavaPlatform defaultPlatform = JavaPlatformManager
220: .getDefault().getDefaultPlatform();
221:
222: if (defaultPlatform != null) {
223: supportedPlatforms.add(new TargetPlatform(
224: defaultPlatform, true));
225: }
226: } catch (Exception e) {
227: // IGNORE
228: e.printStackTrace();
229: }
230:
231: JavaPlatform[] platforms = JavaPlatformManager
232: .getDefault()
233: .getPlatforms(null, new Specification("j2se", null)); // NOI18N
234:
235: for (int i = 0; i < platforms.length; i++) {
236: TargetPlatform platform = new TargetPlatform(
237: platforms[i]);
238:
239: if (!platform.isValid()
240: || supportedPlatforms.contains(platform)) {
241: continue;
242: }
243:
244: supportedPlatforms.add(platform);
245: }
246: }
247:
248: return supportedPlatforms;
249: }
250: }
|