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.j2me.cdc.platform.sun;
043:
044: import java.io.File;
045: import java.io.FileNotFoundException;
046: import java.io.IOException;
047: import java.io.InputStream;
048: import java.util.ArrayList;
049: import java.util.Collections;
050: import java.util.Enumeration;
051: import java.util.HashMap;
052: import java.util.HashSet;
053: import java.util.List;
054: import java.util.Map;
055: import java.util.Properties;
056: import java.util.Set;
057: import java.util.StringTokenizer;
058: import org.netbeans.modules.j2me.cdc.platform.CDCDevice;
059: import org.netbeans.modules.j2me.cdc.platform.CDCPlatform;
060: import org.netbeans.modules.j2me.cdc.platform.spi.CDCPlatformDetector;
061: import org.netbeans.modules.j2me.cdc.platform.spi.CDCPlatformUtil;
062: import org.netbeans.modules.j2me.cdc.platform.spi.StreamReader;
063: import org.openide.ErrorManager;
064: import org.openide.filesystems.FileObject;
065: import org.openide.filesystems.FileUtil;
066:
067: /**
068: *
069: * @author suchys
070: */
071: public class SunPlatformDetector extends CDCPlatformDetector {
072:
073: /** Creates a new instance of CDCPlatform */
074: public SunPlatformDetector() {
075: }
076:
077: public String getPlatformName() {
078: return "SavaJe"; //NOI18N
079: }
080:
081: public String getPlatformType() {
082: return "savaje";
083: }
084:
085: public boolean accept(FileObject dir) {
086: FileObject tool = CDCPlatformUtil.findTool("bin", "emulator",
087: Collections.singleton(dir)); //NOI18N
088: FileObject tool2 = CDCPlatformUtil.findTool("lib",
089: "emulator.properties", Collections.singleton(dir)); //NOI18N
090: return tool != null && tool2 != null;
091: }
092:
093: public CDCPlatform detectPlatform(FileObject dir)
094: throws IOException {
095: assert dir != null;
096: FileObject java = CDCPlatformUtil.findTool("bin", "emulator",
097: Collections.singleton(dir)); //NOI18N
098: if (java == null) {
099: throw new IOException(
100: "emulator.exe can not be found in desired location!"); //NOI18N
101: }
102: File javaFile = FileUtil.toFile(java);
103: if (javaFile == null)
104: throw new IOException(
105: "emulator.exe can not be found in desired location!"); //NOI18N
106: String javapath = javaFile.getAbsolutePath();
107:
108: FileObject bin = dir.getFileObject("bin"); //NOI18N
109:
110: StringBuffer sb = new StringBuffer();
111: try {
112: String[] command = new String[2];
113: command[0] = javapath;
114: command[1] = "-version"; //NOI18N
115: final Process process = Runtime.getRuntime().exec(command,
116: null, FileUtil.toFile(bin));
117: StreamReader ior = new StreamReader(process
118: .getInputStream(), sb);
119: StringBuffer err = new StringBuffer();
120: StreamReader irr = new StreamReader(process
121: .getErrorStream(), err);
122:
123: // PENDING -- this may be better done by using ExecEngine, since
124: // it produces a cancellable task.
125: ior.join();
126: irr.join();
127: process.waitFor();
128: process.getOutputStream().close();
129: int exitValue = process.exitValue();
130: if (exitValue != 0)
131: throw new IOException();
132: } catch (InterruptedException ex) {
133: IOException e = new IOException();
134: ErrorManager.getDefault().annotate(e, ex);
135: throw e;
136: }
137:
138: //search for libraries
139: FileObject libBin = dir.getFileObject("lib"); //NOI18N
140: FileObject foProps = libBin.getFileObject("emulator",
141: "properties"); //NOI18N
142:
143: String name = sb.toString();
144: Properties props = new Properties();
145: String bcp;
146: InputStream is = foProps.getInputStream();
147: try {
148: props.load(is);
149: } finally {
150: if (is != null) {
151: is.close();
152: }
153: }
154:
155: name = props.get("emulator.name") != null ? (String) props
156: .get("emulator.name") : name; //NOI18N
157:
158: List devices = new ArrayList();
159: String dvcs = (String) props.get("device.list"); //NOI18N
160: assert dvcs != null;
161: StringTokenizer st = new StringTokenizer(dvcs, ",");
162:
163: Map modes = new HashMap();
164: modes.put(CDCPlatform.PROP_EXEC_MAIN, null);
165: modes.put(CDCPlatform.PROP_EXEC_XLET, null);
166: // modes.put(CDCPlatform.PROP_EXEC_APPLET, null); //no applet, there is no PP !!!
167:
168: while (st.hasMoreTokens()) {
169: String deviceName = st.nextToken().trim();
170: String apis = (String) props.get("api.list"); //NOI18N
171: assert apis != null;
172: StringTokenizer stapis = new StringTokenizer(apis, ","); //NOI18N
173: List profiles = new ArrayList();
174: while (stapis.hasMoreTokens()) {
175: String api = stapis.nextToken().trim();
176: CDCDevice.CDCProfile profile = new CDCDevice.CDCProfile(
177: api, api, api.substring(api.indexOf('-') + 1),
178: modes, getClassPathForConfiguration(libBin,
179: (String) props.get(api
180: + ".classpath.build")),
181: getClassPathForConfiguration(libBin,
182: (String) props.get(api
183: + ".classpath.run")), api
184: .startsWith("AGUI") ? true : false);
185: profiles.add(profile);
186: }
187: devices.add(new CDCDevice(deviceName, deviceName,
188: (CDCDevice.CDCProfile[]) profiles
189: .toArray(new CDCDevice.CDCProfile[profiles
190: .size()]), null));
191: }
192:
193: //CDCDevice.Screen screen = new CDCDevice.Screen(width, height, bitDepth, isColor, "false", "true"); //NOI18N
194:
195: List jdocs = new ArrayList();
196: FileObject base = dir.getFileObject("docs"); //NOI18N
197: if (base != null) {
198: findJavaDoc(base, jdocs);
199: }
200:
201: return new CDCPlatform(name, name, getPlatformType(),
202: "1.4", //NOI18N
203: Collections.singletonList(dir.getURL()),
204: Collections.EMPTY_LIST, jdocs, (CDCDevice[]) devices
205: .toArray(new CDCDevice[devices.size()]), true);
206: }
207:
208: private static void findJavaDoc(FileObject folder, List folders) {
209: if (folder == null)
210: return;
211: FileObject[] fo = folder.getChildren();
212: for (int i = 0; i < fo.length; i++) {
213: if (fo[i].isData() && "index".equals(fo[i].getName())) {
214: folders.add(fo[i].getParent());
215: }
216: }
217: for (int i = 0; i < fo.length; i++) {
218: if (fo[i].isFolder()
219: && !folders.contains(fo[i].getParent())) {
220: findJavaDoc(fo[i], folders);
221: }
222: }
223: }
224:
225: private String getClassPathForConfiguration(FileObject libBin,
226: String items) {
227: Set set = new HashSet();
228: StringTokenizer st = new StringTokenizer(items, ";");
229: while (st.hasMoreTokens()) {
230: set.add(st.nextToken());
231: }
232:
233: StringBuffer bcp = new StringBuffer();
234: for (Enumeration children = libBin.getChildren(true); children
235: .hasMoreElements();) {
236: FileObject elem = (FileObject) children.nextElement();
237: if (set.contains(elem.getNameExt())) {
238: bcp.append(FileUtil.toFile(elem).getAbsolutePath());
239: if (children.hasMoreElements()) {
240: bcp.append(';');
241: }
242: }
243: }
244: return bcp.toString();
245: }
246:
247: public int getVersion() {
248: return 1;
249: }
250: }
|