KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > jmx > remote > comm > ServletConnection


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /* CVS information
25  * $Header: /cvs/glassfish/jmx-remote/rjmx-impl/src/java/com/sun/enterprise/admin/jmx/remote/comm/ServletConnection.java,v 1.4 2005/12/25 04:26:31 tcfujii Exp $
26  * $Revision: 1.4 $
27  * $Date: 2005/12/25 04:26:31 $
28 */

29
30 package com.sun.enterprise.admin.jmx.remote.comm;
31
32 //jdk imports
33
import java.io.BufferedInputStream JavaDoc;
34 import java.io.BufferedOutputStream JavaDoc;
35 import java.io.ByteArrayOutputStream JavaDoc;
36 import java.io.ObjectInputStream JavaDoc;
37 import java.io.ObjectOutputStream JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.Serializable JavaDoc;
40 import java.net.URLConnection JavaDoc;
41 import java.net.HttpURLConnection JavaDoc;
42 import java.net.URL JavaDoc;
43 /* BEGIN -- S1WS_MOD */
44 import java.io.InputStream JavaDoc;
45 import java.io.OutputStream JavaDoc;
46 import java.util.logging.Logger JavaDoc;
47 /* END -- S1WS_MOD */
48
49 import com.sun.enterprise.admin.jmx.remote.DefaultConfiguration;
50 /* BEGIN -- S1WS_MOD */
51 import com.sun.enterprise.admin.jmx.remote.streams.*;
52 /* END -- S1WS_MOD */
53
54 /** A packager private Class (implementing {@link IConnection} communicating
55  * with Servlet over HTTP. Internally it uses java.net.URLConnection,
56  * for we may need to use it for both HTTP and HTTPS. In case of
57  * java.net.HttpURLConnection, only HTTP can be used. The server side (servlet etc)
58  * has to be configured as documented, for this connection to work.
59  * @author Kedar Mhaswade
60  * @since S1AS8.0
61  * @version 1.0
62  */

63
64 class ServletConnection implements IConnection, Runnable JavaDoc {
65     static final String JavaDoc UNKNOWN_HOST = "Unknown host : ";
66     static final String JavaDoc INVALID_HOST_PORT = "Unable to connect to admin-server. Please check if the server is up and running and that the host and port provided are correct.";
67     static final String JavaDoc UNAUTHORIZED_ACCESS =
68     "Invalid user or password";
69     
70     private URLConnection JavaDoc mConnection = null;
71     private ObjectOutputStream JavaDoc mObjectOutStream = null;
72     private ObjectInputStream JavaDoc mObjectInStream = null;
73     
74 /* BEGIN -- S1WS_MOD */
75     private static final Logger JavaDoc logger = Logger.getLogger(
76         DefaultConfiguration.JMXCONNECTOR_LOGGER);/*,
77         DefaultConfiguration.LOGGER_RESOURCE_BUNDLE_NAME );*/

78
79     private boolean response_received =false;
80     private int streamSize = 0;
81     private int reqSize = 0;
82 /* END -- S1WS_MOD */
83
84     ServletConnection(HttpConnectorAddress a) throws IOException JavaDoc {
85         try{
86 /* BEGIN -- S1WS_MOD */
87             //mConnection = a.openConnection(DefaultConfiguration.DEFAULT_SERVLET_CONTEXT_ROOT);
88
String JavaDoc uri = a.getPath();
89             if (uri == null || uri.trim().length() == 0)
90                 uri = DefaultConfiguration.DEFAULT_SERVLET_CONTEXT_ROOT;
91             mConnection = a.openConnection(uri);
92 /* END -- S1WS_MOD */
93         }
94         catch (IOException JavaDoc ioe){
95             handleException(ioe);
96         }
97     }
98     
99     
100     /**
101      Read an incoming Object.
102      */

103     public Object JavaDoc receive( ) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
104         Object JavaDoc value = null;
105         try {
106             InputStream JavaDoc in = mConnection.getInputStream();
107             mObjectInStream = new ObjectInputStream JavaDoc(in);
108             value = mObjectInStream.readObject();
109             response_received = true;
110             JMXInbandStream.setOutputStream(null, 0);
111             StreamMBeanServerResponseMessage res =
112                 (StreamMBeanServerResponseMessage) value;
113             if (res.isStreamAvailable()) {
114                 JMXInbandStream.setIncomingStream(
115                     new JMXChunkedInputStream(in));
116             }
117         }
118         catch (IOException JavaDoc ioe) {
119             handleException(ioe);
120         }
121         return value;
122     }
123     
124     /**
125      Write an object to the connection
126      */

127     public void send(Serializable JavaDoc req)
128             throws IOException JavaDoc {
129         InputStream JavaDoc in = null;
130         if (req instanceof StreamMBeanServerRequestMessage) {
131             StreamMBeanServerRequestMessage reqm =
132                     (StreamMBeanServerRequestMessage) req;
133             JMXInbandStream.setIncomingStream(null);
134             in = JMXInbandStream.getOutgoingStream();
135             if (in != null) {
136                 reqm.setStreamAvailable(true);
137                 int len = (int) JMXInbandStream.getOutgoingStreamLength();
138
139                 ByteArrayOutputStream JavaDoc byO = new ByteArrayOutputStream JavaDoc();
140                 ObjectOutputStream JavaDoc oO = new ObjectOutputStream JavaDoc(byO);
141                 oO.writeObject(reqm);
142                 reqSize = byO.size();
143                 byO.reset();
144
145                 int chunks = (len/8192) + 2;
146                 streamSize = reqSize + len + (chunks * 4);
147                 ((HttpURLConnection JavaDoc)mConnection).setFixedLengthStreamingMode(streamSize);
148                 mConnection.setRequestProperty("Connection", "Close");
149             }
150         }
151         sendReq(req);
152         if (in != null)
153             sendStream();
154     }
155
156     private void sendStream() {
157 /* Thread thr = new Thread(this);
158         thr.start();
159 */

160         run();
161     }
162
163     public void run() {
164         OutputStream JavaDoc out = null;
165         try {
166             out = new JMXChunkedOutputStream(mConnection.getOutputStream());
167             InputStream JavaDoc in = JMXInbandStream.getOutgoingStream();
168             byte[] bytes = new byte[8192];
169             int len = 0;
170             int prLen = 0;
171             int flBytes = 0;
172             while ((len = in.read(bytes)) != -1) {
173                 out.write(bytes, 0, len);
174             }
175             JMXInbandStream.setOutputStream(null, 0);
176             out.flush();
177             ((JMXChunkedOutputStream)out).writeEOF(reqSize);
178         } catch (Exception JavaDoc ex) {
179             ex.printStackTrace();
180         }
181     }
182
183     private void sendReq( Serializable JavaDoc object ) throws IOException JavaDoc {
184         response_received = false;
185         try {
186             mObjectOutStream = new ObjectOutputStream JavaDoc(
187             new BufferedOutputStream JavaDoc(mConnection.getOutputStream()));
188             mObjectOutStream.writeObject(object);
189             mObjectOutStream.flush();
190             //mObjectOutStream.close();
191
}
192         catch (IOException JavaDoc ioe) {
193             handleException(ioe);
194         }
195     }
196     
197     public void close() {
198         try {
199             mObjectInStream.close();
200             mObjectOutStream.close();
201         }
202         catch(Exception JavaDoc e) {
203             throw new RuntimeException JavaDoc (e);
204         }
205     }
206     
207     
208     private void handleException(IOException JavaDoc e) throws IOException JavaDoc {
209         IOException JavaDoc exception = null;
210         if (e instanceof java.net.UnknownHostException JavaDoc) {
211             exception = new java.net.UnknownHostException JavaDoc(UNKNOWN_HOST +
212             e.getMessage());
213         }
214         else if (e instanceof java.net.ConnectException JavaDoc) {
215             exception = new java.net.ConnectException JavaDoc(INVALID_HOST_PORT);
216         }
217         else {
218             int responseCode =
219             ((HttpURLConnection JavaDoc)mConnection).getResponseCode();
220             if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
221                 exception = new IOException JavaDoc(UNAUTHORIZED_ACCESS);
222             }
223             else {
224                 exception = e;
225             }
226         }
227         throw exception;
228     }
229 }
230
Popular Tags