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: /*
043: * DeviceAnywhereUploadTask.java
044: *
045: * Created on April 27, 2007, 6:01 PM
046: */
047:
048: package org.netbeans.modules.deployment.deviceanywhere;
049:
050: import java.io.BufferedInputStream;
051: import java.io.BufferedReader;
052: import java.io.ByteArrayInputStream;
053: import java.io.DataInputStream;
054: import java.io.File;
055: import java.io.FileInputStream;
056: import java.io.IOException;
057: import java.io.InputStreamReader;
058: import java.util.List;
059: import java.util.ResourceBundle;
060: import org.apache.tools.ant.*;
061: import org.apache.tools.ant.types.*;
062:
063: import org.netbeans.modules.deployment.deviceanywhere.service.ApplicationAPIDeviceWrapper;
064: import org.netbeans.modules.deployment.deviceanywhere.service.ApplicationAPIGetLockedDevicesReturn;
065: import org.netbeans.modules.deployment.deviceanywhere.service.ApplicationAPIStartDownloadScriptReturn;
066: import org.netbeans.modules.deployment.deviceanywhere.service.ApplicationAPIUploadApplicationReturn;
067: import org.netbeans.modules.deployment.deviceanywhere.service.ReturnCodes;
068:
069: /**
070: * @author suchys
071: */
072: public class DeviceAnywhereUploadTask extends Task {
073: private ResourceBundle bundle;
074:
075: private String user;
076: private String password;
077: private int deviceId;
078: private File jadFile;
079: private File jarFile;
080: private String career;
081:
082: @Override
083: public void execute() throws BuildException {
084: bundle = ResourceBundle
085: .getBundle("org/netbeans/modules/deployment/deviceanywhere/messages"); //NOI18N
086:
087: ClassLoader oldClassLoader = Thread.currentThread()
088: .getContextClassLoader();
089: Thread.currentThread().setContextClassLoader(
090: DeviceAnywhereUploadTask.class.getClassLoader());
091:
092: System.setProperty("javax.xml.stream.XMLInputFactory",
093: "com.sun.xml.stream.ZephyrParserFactory"); //NOI18N
094: System.setProperty("javax.xml.stream.XMLOutputFactory",
095: "com.sun.xml.stream.ZephyrWriterFactory"); //NOI18N
096: try {
097: org.netbeans.modules.deployment.deviceanywhere.service.ApplicationAPIService serviceApi = new org.netbeans.modules.deployment.deviceanywhere.service.ApplicationAPIService();
098: org.netbeans.modules.deployment.deviceanywhere.service.ApplicationAPI port = serviceApi
099: .getApplicationAPI();
100: boolean deviceLockedOK = false;
101: try {
102: ApplicationAPIGetLockedDevicesReturn lockedDevices = port
103: .getLockedDevices(user, password);
104: handleReturnCode(lockedDevices.getReturnCode());
105: List<ApplicationAPIDeviceWrapper> result = lockedDevices
106: .getDeviceWrappers().getDeviceWrappers();
107: log("Found following devices:", Project.MSG_VERBOSE); //NOI18N
108: for (ApplicationAPIDeviceWrapper elem : result) {
109: log("Device: " + String.valueOf(elem.getId()),
110: Project.MSG_VERBOSE);
111: if (deviceId == elem.getId()) {
112: deviceLockedOK = true;
113: log("Device " + deviceId + " found",
114: Project.MSG_VERBOSE); //NOI18N
115: }
116: }
117: } catch (Exception ex) {
118: if (ex instanceof ClassNotFoundException) {
119: throw new BuildException(ex);
120: }
121: throw new BuildException(bundle
122: .getString("can_not_connect"));
123: }
124: if (!deviceLockedOK) {
125: throw new BuildException(bundle
126: .getString("device_not_locked"));
127: }
128:
129: byte[] jadData = null;
130: byte[] jarData = null;
131: DataInputStream jad = null;
132: DataInputStream jar = null;
133: try {
134: jad = new DataInputStream(new BufferedInputStream(
135: new FileInputStream(jadFile)));
136: jadData = new byte[jad.available()];
137: jad.read(jadData);
138: //hack for Sprint
139: StringBuffer filtered = new StringBuffer();
140: BufferedReader br = new BufferedReader(
141: new InputStreamReader(new ByteArrayInputStream(
142: jadData)));
143: String line;
144: while ((line = br.readLine()) != null) {
145: if (line.startsWith("MicroEdition-")) //NOI18N
146: continue;
147: filtered.append(line).append("\r\n");
148: }
149: jadData = filtered.toString().getBytes();
150: //end hack for Sprint
151: log("JAD file read", Project.MSG_VERBOSE);
152: } catch (Exception ex) {
153: throw new BuildException(bundle
154: .getString("error_reading_jad"));
155: } finally {
156: if (jad != null) {
157: try {
158: jad.close();
159: } catch (IOException ex) {
160: }
161: }
162: }
163:
164: try {
165: jar = new DataInputStream(new BufferedInputStream(
166: new FileInputStream(jarFile)));
167: jarData = new byte[jar.available()];
168: jar.read(jarData);
169: log("JAR file read", Project.MSG_VERBOSE);
170: } catch (Exception ex) {
171: throw new BuildException(bundle
172: .getString("error_reading_jar"));
173: } finally {
174: if (jar != null) {
175: try {
176: jar.close();
177: } catch (IOException ex) {
178: }
179: }
180: }
181:
182: String name = jadFile.getName();
183: int index = name.indexOf('.');
184: name = name.substring(0, index != -1 ? index : name
185: .length() - 1);
186: int applicationId = -1;
187: try {
188: log("Starting upload", Project.MSG_VERBOSE); //NOI18N
189: //System.out.println(new String(jadData));
190: ApplicationAPIUploadApplicationReturn uploadReturn = port
191: .uploadApplication(user, password, name,
192: jarData, jadData);
193: handleReturnCode(uploadReturn.getReturnCode());
194: applicationId = uploadReturn.getApplicationId();
195: log("Application id: " + applicationId,
196: Project.MSG_VERBOSE); //NOI18N
197: } catch (Exception e) {
198: throw new BuildException(bundle
199: .getString("error_uploading_data"));
200: }
201:
202: try {
203: log("Starting script", Project.MSG_VERBOSE); //NOI18N
204: ApplicationAPIStartDownloadScriptReturn scriptReturn = port
205: .startDownloadScript(user, password, deviceId,
206: applicationId);
207: handleReturnCode(scriptReturn.getReturnCode());
208: log("Script executed", Project.MSG_VERBOSE); //NOI18N
209: } catch (Exception e) {
210: throw new BuildException(bundle
211: .getString("error_running_remote_script"));
212: }
213: } catch (IOException ioEx) {
214: throw new BuildException(ioEx);
215: } finally {
216: System.setProperty("javax.xml.stream.XMLInputFactory", ""); //NOI18N
217: System.setProperty("javax.xml.stream.XMLOutputFactory", ""); //NOI18N
218: Thread.currentThread()
219: .setContextClassLoader(oldClassLoader);
220: }
221: log("Done!", Project.MSG_VERBOSE); //NOI18N
222: }
223:
224: public void setUser(String user) {
225: this .user = user;
226: }
227:
228: public void setPassword(String password) {
229: this .password = password;
230: }
231:
232: public void setDeviceId(int deviceId) {
233: this .deviceId = deviceId;
234: }
235:
236: public void setJadFile(File jadFile) {
237: this .jadFile = jadFile;
238: }
239:
240: public void setJarFile(File jarFile) {
241: this .jarFile = jarFile;
242: }
243:
244: public String getCareer() {
245: return career;
246: }
247:
248: public void setCareer(String career) {
249: this .career = career;
250: }
251:
252: private void handleReturnCode(int returnCode) {
253: if (returnCode == ReturnCodes.INTERNAL_ERROR) {
254: throw new BuildException(bundle.getString("intenal_error"));
255: }
256: if (returnCode == ReturnCodes.LOGIN_FAILED) {
257: throw new BuildException(bundle.getString("wrong_login"));
258: }
259: if (returnCode == ReturnCodes.INVALID_APPLICATION_NAME) {
260: throw new BuildException(bundle
261: .getString("invalid_application_name"));
262: }
263: if (returnCode == ReturnCodes.JAD_FILE_PARSE_ERROR) {
264: throw new BuildException(bundle
265: .getString("jad_file_cant_parse"));
266: }
267: if (returnCode == ReturnCodes.DEVICE_NOT_FOUND) {
268: throw new BuildException(bundle
269: .getString("device_not_found"));
270: }
271: if (returnCode == ReturnCodes.APPLICATION_NOT_FOUND) {
272: throw new BuildException(bundle
273: .getString("application_not_found"));
274: }
275: }
276: }
|