KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > utility > RunURL


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.utility;
66
67 /*
68  * RunURL.java
69  *
70  * Copyright 1999, 2000, 2001 Jcorporate Ltd.
71  */

72
73 import com.jcorporate.expresso.core.db.DBException;
74
75 import java.io.BufferedReader JavaDoc;
76 import java.io.InputStream JavaDoc;
77 import java.io.InputStreamReader JavaDoc;
78 import java.net.MalformedURLException JavaDoc;
79 import java.net.URL JavaDoc;
80 import java.net.URLConnection JavaDoc;
81
82
83 /**
84  * Program to allow a URL to be run from the command line
85  * Primarily intended to allow the Application Server thread to be started
86  * automatically, but can be used to run any arbitrary URL
87  *
88  * @author Michael Nash
89  */

90 public class RunURL {
91     private static final String JavaDoc thisClass = RunURL.class.getName() + ".";
92
93     /**
94      * Constructor
95      */

96     public RunURL() {
97         super();
98     } /* RunURL() */
99
100     /**
101      * Constructor
102      *
103      * @param URLString
104      * @throws DBException If the URL cannot be executed
105      */

106     public RunURL(String JavaDoc URLString)
107             throws DBException {
108         String JavaDoc myName = (thisClass + "RunURL(String[])");
109         URL JavaDoc myUrl = null;
110         URLConnection JavaDoc c;
111         System.out.println(myName + ":Executing URL:" + URLString);
112
113         try {
114             myUrl = new URL JavaDoc(URLString);
115         } catch (MalformedURLException JavaDoc m) {
116             throw new DBException(myName + ":Bad URL:" + URLString);
117         }
118         try {
119             c = myUrl.openConnection();
120             c.setDoOutput(true);
121             c.setAllowUserInteraction(true);
122
123             Object JavaDoc o2 = c.getContent();
124
125             if (o2 == null) {
126                 throw new DBException(myName + ":Null content reading from " +
127                         "server");
128             }
129             if (o2 instanceof InputStream JavaDoc) {
130                 InputStream JavaDoc is = (InputStream JavaDoc) o2;
131                 BufferedReader JavaDoc dis = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
132
133                 if (dis == null) {
134                     throw new DBException(myName + ":No connection to " +
135                             "server:DataInputStream dis is null");
136                 }
137
138                 int linesRead = 0;
139                 String JavaDoc oneLine = dis.readLine();
140
141                 if (oneLine == null) {
142                     throw new DBException(myName + ":Server never sent EOF");
143                 }
144
145                 System.out.println(oneLine);
146
147                 while (oneLine != null) {
148                     if (!oneLine.equals("")) {
149                         linesRead++;
150                         System.out.println(oneLine);
151                     } /* if */
152
153
154                     oneLine = dis.readLine();
155                 }
156
157                 dis.close();
158             } else { /* while */
159                 throw new DBException(myName + ":Could not get input stream " +
160                         "from servlet");
161             }
162         } /* try */ catch (Exception JavaDoc io) {
163             io.printStackTrace();
164             throw new DBException(myName + ":I/O Error communicating with " +
165                     "server with URL '" + URLString +
166                     "'. Logged in as user " +
167                     System.getProperty("user.name") + ":" +
168                     io.getMessage());
169         }
170
171         System.out.println("URL executed successfully");
172     } /* RunURL(String) */
173
174     /**
175      * Main Method.
176      *
177      * @param args[] Command line arguments. A URL to execute.
178      */

179     public static void main(String JavaDoc[] args) {
180         if (args.length < 1) {
181             System.out.println("You must specify a URL as an argument. " +
182                     "Enclose in quotes");
183             System.exit(1);
184         }
185         try {
186             new RunURL(args[0]);
187         } catch (DBException de) {
188             System.out.println(de.getMessage());
189             System.exit(1);
190         }
191     } /* main(String) */
192
193 } /* RunURL */
194
195 /* RunURL */
Popular Tags