Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
COOS - Connected Objects Operating System (www.connectedobjects.org). Copyright (C) 2009 Telenor ASA and Tellu AS. All rights reserved. DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. This library is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. You may also contact one of the following for additional information: Telenor ASA, Snaroyveien 30, N-1331 Fornebu, Norway (www.telenor.no) Tellu AS, Hagalokkveien 13, N-1383 Asker, Norway (www.tellu.no)
 
 package org.coos.messaging.cldc.transport;
 
 
 import  com.sun.kvem.io.j2me.serversocket.Socket;
 
 import  com.sun.midp.ssl.CertStore;
 import  com.sun.midp.ssl.SSLStreamConnection;
 import  com.sun.midp.ssl.X509Certificate;
 
 import java.io.*;
 
Created by IntelliJ IDEA. User: Knut Eilif SecureTCPTransport implements a SSL connection for CLDC devices
 
 public class SecureTCPTransport extends TCPTransport implements TransportService {
 
 	// new field SSLConnection
 	SSLStreamConnection sslConnection;

Empty constructor to allow serialization and dynamic class loading
 
 	public SecureTCPTransport() {
 		super();
 	}

constructkor
 
 	public SecureTCPTransport(String hostIPint hostPort) {
 		super(hostIPhostPort);
 	}

Starts the transport, I.E. initiates a connection This method is overridden to make the writer and reader initiated with the secureWriter and SecureReader classes, that provide a secure connection
 
 	public void start() throws Exception {
 		 = true;
 
 		if ( != null) {
 			// Socket already exist, Start reader and writer
 			 = new SecureReader();
 			 = new SecureWriter();
 			return;
 		}
 
 		// socket does not exist, must be established before starting reader and
 		// writer
 		if ( == null) {
 			 = (String.get("host");
 		}
 
 		if ( == 0 && .get("port") != null) {
 			 = Integer.parseInt((String.get("port"));
 		}
 
 		Thread t = new Thread(new Runnable() {
 
 			public void run() {
				if ( == null) {
					boolean connecting = true;
					while (connecting && ) {
						try {
							 = (SocketConnection) Connector.open("socket://" +  + ":" + );
							 = new SSLStreamConnection(.openInputStream(),
							// sslConnection.setTrustedCertStore(new
							// MyCertStore());
							connecting = false;
catch (IOException e) {
							String retryStr = (String.get("retry");
							if (retryStr == null || retryStr.equals("true")) {
								// logger.info("Establishing transport to " +
								// hostName + ":" + hostPort +
								// " failed. Retrying in 10 sec.");
								try {
									Thread.sleep(10000);
catch (InterruptedException e1) {
								}
else {
								connecting = false;
							}
						}
					}
					if () {
						// logger.info("Transport from " +
						// socket.getLocalSocketAddress() + " to " +
						// socket.getRemoteSocketAddress() + " established.");
					}
				}
				 = new SecureReader();
				 = new SecureWriter();
			}
		});
		t.start();
	}

the Reader is overridden to use SSL connection
	class SecureReader extends Reader implements Runnable {
		public void run() {
			try {
				// is = socket.getInputStream();
				// instantiate a SSLStreamConnection
				DataInputStream din = new DataInputStream(.openInputStream());
				while () {
					Message msg = null;
					try {
						msg = new DefaultMessage(din);
catch (EOFException e) {
						// logger.info("Connection closing EOF");
						 = false;
catch (Exception e) {
						// logger.fatal("Error in Message deserialization. Aborting");
						 = false;
					}
				}
				if ( != null) {
				}
catch (IOException e) {
			}
		}
	}
	class SecureWriter extends Writer implements Runnable {
		public void run() {
			try {
				// os = socket.getOutputStream();
				 = .openOutputStream();
				while () {
					if (.isEmpty()) {
						synchronized (SecureTCPTransport.this) {
							try {
catch (InterruptedException e) {
								if (!)
									return;
							}
						}
else {
						// Message msg = mailbox.remove(0);
						try {
catch (Exception e) {
							// logger.fatal("Error in Message writing. Aborting");
							 = false;
						}
					}
				}
				if ( != null) {
				}
catch (IOException e) {
			}
		}
	}
New to GrepCode? Check out our FAQ X