View Javadoc

1   /**
2    *  BlueCove - Java library for Bluetooth
3    *  Copyright (C) 2008 Vlad Skarzhevskyy
4    *
5    *  Licensed to the Apache Software Foundation (ASF) under one
6    *  or more contributor license agreements.  See the NOTICE file
7    *  distributed with this work for additional information
8    *  regarding copyright ownership.  The ASF licenses this file
9    *  to you under the Apache License, Version 2.0 (the
10   *  "License"); you may not use this file except in compliance
11   *  with the License.  You may obtain a copy of the License at
12   *
13   *    http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing,
16   *  software distributed under the License is distributed on an
17   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   *  KIND, either express or implied.  See the License for the
19   *  specific language governing permissions and limitations
20   *  under the License.
21   *
22   *  @author vlads
23   *  @version $Id: Deploy.java 2476 2008-12-01 17:41:59Z skarzhevskyy $
24   */
25  package net.sf.bluecove.obex;
26  
27  import java.io.ByteArrayOutputStream;
28  import java.io.File;
29  import java.io.FileInputStream;
30  import java.io.InputStream;
31  import java.net.URL;
32  
33  /**
34   *
35   *
36   */
37  public class Deploy implements UserInteraction {
38  
39  	private String fileName;
40  
41  	private int progressMaximum;
42  
43  	public static void main(String[] args) {
44  		if ((args.length < 2) || args[0].equalsIgnoreCase("--help")) {
45  			StringBuffer usage = new StringBuffer();
46  			usage.append("Usage:\n java ").append(Deploy.class.getName());
47  			usage.append(" bluetoothURL yourApp.jar\n");
48  			System.out.println(usage);
49  			System.exit(1);
50  			return;
51  		}
52  		String obexUrl = args[0];
53  
54  		String filePath = args[1];
55  
56  		Logger.debugOn = false;
57  		Deploy d = new Deploy();
58  		byte[] data = d.readFile(filePath);
59  		if (data == null) {
60  			System.exit(1);
61  			return;
62  		}
63  		ObexBluetoothClient o = new ObexBluetoothClient(d, d.fileName, data);
64  		if (o.obexPut(obexUrl)) {
65  			System.exit(0);
66  		} else {
67  			System.exit(2);
68  		}
69  	}
70  
71  	private Deploy() {
72  	}
73  
74  	private static String simpleFileName(String filePath) {
75  		int idx = filePath.lastIndexOf('/');
76  		if (idx == -1) {
77  			idx = filePath.lastIndexOf('\\');
78  		}
79  		if (idx == -1) {
80  			return filePath;
81  		}
82  		return filePath.substring(idx + 1);
83  	}
84  
85  	private byte[] readFile(final String filePath) {
86  		InputStream is = null;
87  		byte[] data = null;
88  		try {
89  			String path = filePath;
90  			String inputFileName;
91  			File file = new File(filePath);
92  			if (file.exists()) {
93  				is = new FileInputStream(file);
94  				inputFileName = file.getName();
95  			} else {
96  				URL url = new URL(path);
97  				is = url.openConnection().getInputStream();
98  				inputFileName = url.getFile();
99  			}
100 			ByteArrayOutputStream bos = new ByteArrayOutputStream();
101 			byte[] buffer = new byte[0xFF];
102 			int i = is.read(buffer);
103 			int done = 0;
104 			while (i != -1) {
105 				bos.write(buffer, 0, i);
106 				done += i;
107 				// setProgressValue(done);
108 				i = is.read(buffer);
109 			}
110 			data = bos.toByteArray();
111 			fileName = simpleFileName(inputFileName);
112 			showStatus((data.length / 1024) + "k " + fileName);
113 		} catch (Throwable e) {
114 			Logger.error(e);
115 			showStatus("Download error " + e.getMessage());
116 		} finally {
117 			IOUtils.closeQuietly(is);
118 		}
119 		return data;
120 	}
121 
122 	/*
123 	 * (non-Javadoc)
124 	 *
125 	 * @see net.sf.bluecove.obex.UserInteraction#setProgressMaximum(int)
126 	 */
127 	public void setProgressMaximum(int n) {
128 		progressMaximum = n;
129 	}
130 
131 	/*
132 	 * (non-Javadoc)
133 	 *
134 	 * @see net.sf.bluecove.obex.UserInteraction#setProgressValue(int)
135 	 */
136 	public void setProgressValue(int n) {
137 		// TODO Auto-generated method stub
138 
139 	}
140 
141 	/*
142 	 * (non-Javadoc)
143 	 *
144 	 * @see net.sf.bluecove.obex.UserInteraction#setProgressDone()
145 	 */
146 	public void setProgressDone() {
147 		// TODO Auto-generated method stub
148 	}
149 
150 	/*
151 	 * (non-Javadoc)
152 	 *
153 	 * @see net.sf.bluecove.obex.UserInteraction#showStatus(java.lang.String)
154 	 */
155 	public void showStatus(String message) {
156 		System.out.println(message);
157 	}
158 
159 }