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 the Nokia Deployment.
027: * The Initial Developer of the Original Software is Nokia Corporation.
028: * Portions created by Nokia Corporation Copyright 2005, 2007.
029: * All Rights Reserved.
030: *
031: * If you wish your version of this file to be governed by only the CDDL
032: * or only the GPL Version 2, indicate your decision by adding
033: * "[Contributor] elects to include this software in this distribution
034: * under the [CDDL or GPL Version 2] license." If you do not indicate a
035: * single choice of license, a recipient has the option to distribute
036: * your version of this file under either the CDDL, the GPL Version 2 or
037: * to extend the choice of license to its licensees as provided above.
038: * However, if you add GPL Version 2 code and therefore, elected the GPL
039: * Version 2 license, then the option applies only if the new code is
040: * made subject to such option by the copyright holder.
041: */
042:
043: package com.nokia.phone.deploy;
044:
045: import java.io.File;
046: import java.io.IOException;
047: import java.util.StringTokenizer;
048: import java.util.ArrayList;
049: import java.util.List;
050:
051: /**
052: * Provides methods for deployment operations.
053: */
054: public class Deployer {
055:
056: /**
057: * Current platform connection status.
058: */
059: private boolean connected = false;
060:
061: /**
062: * Default constructor, opens the connection layer.
063: */
064: public Deployer() {
065: connected = false;
066:
067: // Open connection
068: openConnectionLayer();
069:
070: if (!connected) {
071: // nothing to do, only inform the user of missing libraries
072: // when he wants to use Nokia deployment
073: }
074: }
075:
076: /**
077: * Returns the current connection status.
078: * @return true if connection to service layer is open, otherwise false
079: */
080: public boolean isConnected() {
081: return connected;
082: }
083:
084: /**
085: * Returns whether PC Suite is installed or not.
086: * @return boolean PC suite installation status.
087: */
088: public boolean isPCSuiteInstalled() {
089: return CONA.getInstance().isConnAPIDllFound();
090: }
091:
092: /**
093: * Returns whether deployment is supported on the current OS or not.
094: * @return boolean deployment support status.
095: */
096: public boolean isOSSupportsDeployment() {
097: return CONA.getInstance().isOSSupportsDeployment();
098: }
099:
100: /**
101: * Opens the platform connection layer using native method.
102: */
103: public void openConnectionLayer() {
104: if (CONA.getInstance().isConnAPIDllFound()
105: && CONA.getInstance().isConnJNIDllFound() && !connected) {
106: connected = CONA.getInstance().connect();
107: }
108: }
109:
110: /**
111: * Closes the platform connection layer using native method.
112: */
113: public void closeConnectionLayer() {
114: try {
115: Thread.sleep(3050);
116: } catch (InterruptedException e) {
117: // No error reporting
118: }
119: if (connected) {
120: boolean disconnected = CONA.getInstance().disconnect();
121: if (!disconnected) {
122: System.err.println("Failed to close service layer");
123: }
124: connected = !disconnected;
125: }
126: }
127:
128: /**
129: * Returns the terminals that are currently connected to the computer. The
130: * name includes an ID number for the session to identify them from each
131: * other.
132: *
133: * @return terminal names as a String array, if no terminals are connected,
134: * returns a zero-length String array.
135: */
136: public List<String> getTerminals() {
137: List<String> terminalList = new ArrayList<String>();
138:
139: if (!connected) {
140: openConnectionLayer();
141: }
142:
143: // if connection failed, print error and return empty list
144: if (!connected) {
145: System.err
146: .println("Connection opening failed, could not get terminal list.");
147: return terminalList;
148: }
149:
150: addDevicesToList(CONA.getInstance().getDevices(
151: CONA.CONAPI_MEDIA_BLUETOOTH), terminalList);
152: addDevicesToList(CONA.getInstance().getDevices(
153: CONA.CONAPI_MEDIA_IRDA), terminalList);
154: addDevicesToList(CONA.getInstance().getDevices(
155: CONA.CONAPI_MEDIA_SERIAL), terminalList);
156: addDevicesToList(CONA.getInstance().getDevices(
157: CONA.CONAPI_MEDIA_USB), terminalList);
158:
159: return terminalList;
160: }
161:
162: /**
163: * Returns the connection type of a particular terminal connected to the
164: * computer.
165: *
166: * @param terminal
167: * name of the terminal, complete with session ID
168: * @return connection type ("RS232", "IrDA", "USB" or "Bluetooth") of the
169: * terminal
170: */
171: public String getConnectionType(String terminal) {
172: if (!connected) {
173: openConnectionLayer();
174: }
175: int id = getID(terminal);
176:
177: return CONA.getInstance().getDeviceType(id);
178: }
179:
180: /**
181: * Deploys the JAR and JAD files to all connected terminals.
182: *
183: * @param jad
184: * JAD file to deploy
185: * @param jar
186: * JAR file to deploy
187: * @return boolean whether deployment was successful or not.
188: * @see #getTerminals
189: */
190: public List<String> deployToAllTerminals(File jad, File jar) {
191: List<String> allTerminals = getTerminals();
192: List<String> notOk = new ArrayList<String>();
193: for (String terminal : allTerminals) {
194: try {
195: boolean success = deploy(jad, jar, terminal);
196: if (success) {
197: System.out.println("Deployment succeeded!");
198: } else {
199: notOk.add(terminal);
200: }
201: } catch (UnsatisfiedLinkError e) {
202: e.printStackTrace();
203: notOk.add(terminal);
204: } catch (IOException e) {
205: e.printStackTrace();
206: notOk.add(terminal);
207: }
208: }
209: return notOk;
210: }
211:
212: /**
213: * Deploys the JAR and JAD files to the selected terminal.
214: *
215: * @param jad
216: * JAD file to deploy
217: * @param jar
218: * JAR file to deploy
219: * @param terminal
220: * to deploy to, use the name you got from {@link #getTerminals}
221: * @throws IOException
222: * if there is an error opening or reading the JAR or JAD file
223: * @throws UnsatisfiedLinkError
224: * if ConnAPI.dll has not been loaded correctly. Either the
225: * DLL was not found or it was an old and incompatible version.
226: * @return boolean whether deployment was successful or not.
227: * @see #getTerminals
228: *
229: */
230: public boolean deploy(File jad, File jar, String terminal)
231: throws IOException, UnsatisfiedLinkError {
232: if (!CONA.getInstance().isConnAPIDllFound()) {
233: throw new UnsatisfiedLinkError("Failed to open ConnAPI.dll");
234: }
235: if (!CONA.getInstance().isConnJNIDllFound()) {
236: throw new UnsatisfiedLinkError("Failed to open ConnJNI.dll");
237: }
238: if (!connected) {
239: openConnectionLayer();
240: if (!connected) {
241: System.err.println("Failed to open service layer");
242: return false;
243: } else {
244: System.out.println("Service layer opened");
245: }
246: }
247:
248: int id = getID(terminal);
249:
250: if (!jad.exists()) {
251: throw new IOException("JAD file not found:"
252: + jad.getAbsolutePath());
253: }
254:
255: if (!jar.exists()) {
256: throw new IOException("JAR file not found:"
257: + jar.getAbsolutePath());
258: }
259:
260: if (id != -1) {
261: return this .synchronizedDeploy(jad, jar, id);
262: } else {
263: System.err.println("Failed to find destination device:"
264: + terminal);
265: return false;
266: }
267: }
268:
269: /**
270: * Opens a connection to the given device using native method.
271: * @param ind the id of the device to open.
272: */
273: private boolean openConnection(int ind) {
274: boolean openC = CONA.getInstance().openFileSystem(ind);
275: if (openC) {
276: return true;
277: }
278:
279: return false;
280: }
281:
282: /**
283: * Closes the connection to device using native method.
284: */
285: private boolean closeConnection() {
286: return CONA.getInstance().closeFileSystem();
287: }
288:
289: /**
290: * Utility method that adds the given device names to the given list.
291: */
292: private void addDevicesToList(String devices, List<String> list) {
293: if (devices == null) {
294: return;
295: }
296: StringTokenizer devicesTokenizer = new StringTokenizer(devices,
297: ","); //$NON-NLS-1$
298: while (devicesTokenizer.hasMoreTokens()) {
299: list.add(devicesTokenizer.nextToken());
300: }
301: }
302:
303: /**
304: * Utility method that returns the ID from the given terminal name.
305: */
306: private int getID(String terminal) {
307: String idString = terminal.substring(
308: terminal.indexOf("(ID:") + 4, //$NON-NLS-1$
309: terminal.indexOf(")")).trim(); //$NON-NLS-1$
310: return Integer.parseInt(idString);
311: }
312:
313: /**
314: * Opens a connection to the given terminal and deploys the given application to it.
315: * @param jad
316: * JAD file to deploy
317: * @param jar
318: * JAR file to deploy
319: * @param id
320: * to id of the terminal to deploy to, use the name you got from {@link #getTerminals}
321: * @throws IOException
322: * if there is an error opening or reading the JAR or JAD file
323: * @throws UnsatisfiedLinkError
324: * if ConnAPI.dll has not been loaded correctly. Either the
325: * DLL was not found or it was an old and incompatible version.
326: * @see #getTerminals
327: */
328: private boolean synchronizedDeploy(File jad, File jar, int id) {
329: boolean success = true;
330: String jarfilepath = jar.getParentFile().getAbsolutePath();
331: String jadfilename = jad.getName();
332: String jarfilename = jar.getName();
333:
334: boolean bopenconnection = openConnection(id);
335:
336: if (bopenconnection) {
337:
338: String strFilepath = jarfilepath + "\\"; //$NON-NLS-1$
339:
340: success = CONA.getInstance().installApplication(
341: strFilepath, jarfilename, jadfilename,
342: CONA.CONA_APPLICATION_TYPE_JAVA, true);
343: } else {
344: // open connection failed
345: return false;
346: }
347:
348: closeConnection();
349: return true;
350: }
351: }
|