use DataInputStream vs ObjectInputStream some simple testing via Client.main method
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@805997 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/sigil/common/runtime/sigil.properties b/sigil/common/runtime/sigil.properties
index 79def6a..af88a95 100644
--- a/sigil/common/runtime/sigil.properties
+++ b/sigil/common/runtime/sigil.properties
@@ -8,6 +8,7 @@
org.apache.felix.sigil.common.runtime.*, \
org.apache.commons.cli.*, \
org.apache.commons.io.*, \
+ org.osgi.framework.launch,\
-exports: \
org.apache.felix.sigil.common.runtime, \
diff --git a/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/Client.java b/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/Client.java
index 61dabd2..dff2c28 100644
--- a/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/Client.java
+++ b/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/Client.java
@@ -20,10 +20,11 @@
package org.apache.felix.sigil.common.runtime;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
import java.net.InetAddress;
+import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Map;
import java.util.Properties;
@@ -48,22 +49,50 @@
public static final String ADDRESS_PROPERTY = "address";
private Socket socket;
- private InputStream in;
- private OutputStream out;
+ private DataInputStream in;
+ private DataOutputStream out;
public Client()
{
}
+
+ public static void main(String[] args) throws IOException, BundleException {
+ Client cl = new Client();
+ Properties props = new Properties();
+ props.put( ADDRESS_PROPERTY, "localhost" );
+ props.put( PORT_PROPERTY, "9090" );
+ cl.connect( props );
+ System.out.println( cl.status() );
+ long id = cl.install( "file:/Users/dave/.m2/repository/org/apache/felix/org.apache.felix.log/1.1.0-SNAPSHOT/org.apache.felix.log-1.1.0-SNAPSHOT.jar" );
+ System.out.println( cl.status() );
+ cl.start( id );
+ System.out.println( cl.status() );
+
+ id = cl.install( "file:/Users/dave/.m2/repository/org/apache/felix/org.apache.felix.shell/1.3.0-SNAPSHOT/org.apache.felix.shell-1.3.0-SNAPSHOT.jar" );
+ cl.start( id );
+
+ id = cl.install( "file:/Users/dave/.m2/repository/org/apache/felix/org.apache.felix.shell.tui/1.3.0-SNAPSHOT/org.apache.felix.shell.tui-1.3.0-SNAPSHOT.jar" );
+ cl.start( id );
+
+ System.out.println( cl.status() );
+ }
public void connect(Properties props) throws IOException
{
- InetAddress address = InetAddress.getByName( props.getProperty( ADDRESS_PROPERTY ) );
+ String v = props.getProperty( ADDRESS_PROPERTY );
+ InetAddress address = v == null ? null : InetAddress.getByName( v );
int port = Integer.parseInt( props.getProperty( PORT_PROPERTY, "0" ) );
- socket = new Socket( address, port );
- in = socket.getInputStream();
- out = socket.getOutputStream();
+ InetSocketAddress endpoint = new InetSocketAddress(address, port);
+
+ socket = new Socket();
+ socket.connect( endpoint );
+
+ Main.log( "Connected to " + endpoint );
+
+ in = new DataInputStream( socket.getInputStream() );
+ out = new DataOutputStream( socket.getOutputStream() );
}
@@ -81,19 +110,19 @@
public void start( long bundle ) throws IOException, BundleException
{
- new StartAction( in, out ).client();
+ new StartAction( in, out ).client( bundle );
}
public void stop( long bundle ) throws IOException, BundleException
{
- new StopAction( in, out ).client();
+ new StopAction( in, out ).client( bundle );
}
public void uninstall( long bundle ) throws IOException, BundleException
{
- new UninstallAction( in, out ).client();
+ new UninstallAction( in, out ).client( bundle );
}
diff --git a/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/Main.java b/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/Main.java
index 85a8f7f..237d400 100644
--- a/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/Main.java
+++ b/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/Main.java
@@ -53,6 +53,7 @@
options.addOption( "?", "help", false, "Print help for the Sigil launcher" );
options.addOption( "p", "port", true, "Port to launch server on (0 implies auto allocate) [default 0]" );
options.addOption( "a", "address", true, "Address to bind server to [default all]" );
+ options.addOption( "c", "clean", false, "Clean bundle cache directory on init" );
}
@@ -111,6 +112,10 @@
e.printStackTrace();
System.exit( 1 );
}
+ catch (Error e) {
+ e.printStackTrace();
+ throw e;
+ }
}
@@ -154,6 +159,8 @@
private static Map<String, String> buildConfig( CommandLine cl )
{
HashMap<String, String> config = new HashMap<String, String>();
+ if ( cl.hasOption( 'c' ))
+ config.put( "org.osgi.framework.storage.clean", "onFirstInit" );
return config;
}
@@ -174,4 +181,8 @@
HelpFormatter f = new HelpFormatter();
f.printHelp( COMMAND_LINE_SYNTAX, options );
}
+
+ public static void log(String msg) {
+ System.out.println( msg );
+ }
}
diff --git a/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/Server.java b/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/Server.java
index 04724ef..509ba38 100644
--- a/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/Server.java
+++ b/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/Server.java
@@ -20,14 +20,14 @@
package org.apache.felix.sigil.common.runtime;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.EOFException;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
-import java.net.SocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -70,7 +70,7 @@
socket.bind( socketAddress );
- System.out.println( "Started server listening on " + socket.getLocalSocketAddress() + ":" + socket.getLocalPort() );
+ Main.log( "Started server listening on " + socket.getLocalSocketAddress() + ":" + socket.getLocalPort() );
accept = new Thread( new Runnable()
{
@@ -90,6 +90,10 @@
}
}
} );
+
+ accept.start();
+
+ Main.log( "Started thread!" );
}
@@ -120,14 +124,19 @@
*/
public void run()
{
+ Main.log( "Accepted " + socket.getInetAddress() + ":" + socket.getLocalPort() );
+
try
{
- InputStream in = socket.getInputStream();
- OutputStream out = socket.getOutputStream();
+ DataInputStream in = new DataInputStream( socket.getInputStream() );
+ DataOutputStream out = new DataOutputStream( socket.getOutputStream() );
+
while ( !stopped.get() )
{
- int action = in.read();
+ int action = in.readInt();
+
Action<?, ?> task = null;
+
switch ( action )
{
case INSTALL:
@@ -149,14 +158,34 @@
task = new StatusAction( in, out );
break;
}
- task.server( fw );
+
+ if ( task == null ) {
+ Main.log( "Invalid action " + action );
+ }
+ else {
+ task.server( fw );
+ }
}
}
+ catch ( EOFException e ) {
+ // fine
+ }
catch ( IOException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
+ finally {
+ try
+ {
+ socket.close();
+ }
+ catch ( IOException e )
+ {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
}
}
}
diff --git a/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/Action.java b/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/Action.java
index 0e6ceb5..abf0c94 100644
--- a/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/Action.java
+++ b/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/Action.java
@@ -20,11 +20,10 @@
package org.apache.felix.sigil.common.runtime.io;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
@@ -32,6 +31,7 @@
import org.osgi.framework.launch.Framework;
import org.apache.commons.io.input.CountingInputStream;
+import org.apache.felix.sigil.common.runtime.Main;
import static org.apache.felix.sigil.common.runtime.io.Constants.OK;
import static org.apache.felix.sigil.common.runtime.io.Constants.ERROR;
@@ -43,14 +43,15 @@
*/
public abstract class Action<I, O>
{
- private final ObjectInputStream in;
- private final ObjectOutputStream out;
+ private static final String ASCII = "ASCII";
+ private final DataInputStream in;
+ private final DataOutputStream out;
- public Action( InputStream in, OutputStream out ) throws IOException
+ public Action( DataInputStream in, DataOutputStream out ) throws IOException
{
- this.in = new ObjectInputStream( in );
- this.out = new ObjectOutputStream( out );
+ this.in = in;
+ this.out = out;
}
@@ -95,13 +96,18 @@
protected String readString() throws IOException
{
- return in.readUTF();
+ int l = in.readInt();
+ byte[] buf = new byte[l];
+ in.readFully( buf );
+ return new String(buf, ASCII);
}
protected void writeString( String str ) throws IOException
{
- out.writeUTF( str );
+ byte[] buf = str.getBytes( ASCII );
+ out.writeInt( buf.length );
+ out.write( buf );
}
@@ -118,7 +124,7 @@
protected void writeLong( long l ) throws IOException
- {
+ {
out.writeLong( l );
}
@@ -197,4 +203,8 @@
{
out.flush();
}
+
+ protected void log(String msg) {
+ Main.log( msg );
+ }
}
diff --git a/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/Constants.java b/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/Constants.java
index 58a614d..1d1e788 100644
--- a/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/Constants.java
+++ b/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/Constants.java
@@ -27,8 +27,8 @@
public static final int INSTALL = 1;
public static final int START = 2;
- public static final int STOP = 4;
- public static final int UNINSTALL = 8;
- public static final int UPDATE = 16;
- public static final int STATUS = 32;
+ public static final int STOP = 3;
+ public static final int UNINSTALL = 4;
+ public static final int UPDATE = 5;
+ public static final int STATUS = 6;
}
diff --git a/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/InstallAction.java b/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/InstallAction.java
index fd43a20..4156197 100644
--- a/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/InstallAction.java
+++ b/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/InstallAction.java
@@ -20,9 +20,9 @@
package org.apache.felix.sigil.common.runtime.io;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
@@ -38,7 +38,7 @@
public class InstallAction extends Action<String, Long>
{
- public InstallAction( InputStream in, OutputStream out ) throws IOException
+ public InstallAction( DataInputStream in, DataOutputStream out ) throws IOException
{
super( in, out );
}
@@ -49,6 +49,7 @@
{
writeInt( INSTALL );
writeString( url );
+ flush();
if ( checkOk() )
{
return readLong();
@@ -68,6 +69,7 @@
try
{
Bundle val = fw.getBundleContext().installBundle( url );
+ log( "Installed " + url );
writeOk();
writeLong( val.getBundleId() );
}
@@ -76,7 +78,7 @@
writeError();
writeString( e.getMessage() );
}
-
+ flush();
}
}
diff --git a/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/StartAction.java b/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/StartAction.java
index 3590223..6790f74 100644
--- a/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/StartAction.java
+++ b/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/StartAction.java
@@ -20,9 +20,9 @@
package org.apache.felix.sigil.common.runtime.io;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
@@ -38,7 +38,7 @@
public class StartAction extends Action<Long, Void>
{
- public StartAction( InputStream in, OutputStream out ) throws IOException
+ public StartAction( DataInputStream in, DataOutputStream out ) throws IOException
{
super( in, out );
}
@@ -49,6 +49,7 @@
{
writeInt( START );
writeLong( bundle );
+ flush();
if ( !checkOk() )
{
String msg = readString();
@@ -74,6 +75,7 @@
{
b.start();
writeOk();
+ log( "Started " + b.getSymbolicName() );
}
catch ( BundleException e )
{
@@ -81,6 +83,7 @@
writeString( e.getMessage() );
}
}
+ flush();
}
}
diff --git a/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/StatusAction.java b/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/StatusAction.java
index f5379d1..5b22c07 100644
--- a/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/StatusAction.java
+++ b/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/StatusAction.java
@@ -20,9 +20,9 @@
package org.apache.felix.sigil.common.runtime.io;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
@@ -40,7 +40,7 @@
public class StatusAction extends Action<Void, Map<Long, String>>
{
- public StatusAction( InputStream in, OutputStream out ) throws IOException
+ public StatusAction( DataInputStream in, DataOutputStream out ) throws IOException
{
super( in, out );
}
@@ -50,6 +50,7 @@
public Map<Long, String> client( Void in ) throws IOException
{
writeInt(STATUS);
+ flush();
int num = readInt();
HashMap<Long, String> map = new HashMap<Long, String>(num);
@@ -66,13 +67,30 @@
@Override
public void server( Framework fw ) throws IOException
{
+ log( "Read status" );
Bundle[] bundles = fw.getBundleContext().getBundles();
writeInt( bundles.length );
for ( Bundle b : bundles ) {
writeLong(b.getBundleId());
- String symbol = b.getSymbolicName() + ":" + b.getHeaders().get( BUNDLE_VERSION );
+ String symbol = b.getSymbolicName() + ":" + b.getHeaders().get( BUNDLE_VERSION ) + ":" + state(b);
writeString(symbol);
}
+
+ flush();
+ }
+
+
+ private String state( Bundle b )
+ {
+ switch ( b.getState() ) {
+ case Bundle.ACTIVE: return "active";
+ case Bundle.INSTALLED: return "installed";
+ case Bundle.RESOLVED: return "resolved";
+ case Bundle.STARTING: return "starting";
+ case Bundle.STOPPING: return "stopping";
+ case Bundle.UNINSTALLED: return "uninstalled";
+ default: return "unknown";
+ }
}
}
diff --git a/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/StopAction.java b/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/StopAction.java
index 4735eb7..9588a25 100644
--- a/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/StopAction.java
+++ b/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/StopAction.java
@@ -20,9 +20,9 @@
package org.apache.felix.sigil.common.runtime.io;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
@@ -38,9 +38,10 @@
public class StopAction extends Action<Long, Void>
{
- public StopAction( InputStream in, OutputStream out ) throws IOException
+ public StopAction( DataInputStream in, DataOutputStream out ) throws IOException
{
super( in, out );
+ // TODO Auto-generated constructor stub
}
diff --git a/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/UninstallAction.java b/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/UninstallAction.java
index c60d1c5..0abb739 100644
--- a/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/UninstallAction.java
+++ b/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/UninstallAction.java
@@ -20,9 +20,9 @@
package org.apache.felix.sigil.common.runtime.io;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
@@ -38,10 +38,9 @@
public class UninstallAction extends Action<Long, Void>
{
- public UninstallAction( InputStream in, OutputStream out ) throws IOException
+ public UninstallAction( DataInputStream in, DataOutputStream out ) throws IOException
{
super( in, out );
- // TODO Auto-generated constructor stub
}
diff --git a/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/UpdateAction.java b/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/UpdateAction.java
index 6d56468..99b684a 100644
--- a/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/UpdateAction.java
+++ b/sigil/common/runtime/src/org/apache/felix/sigil/common/runtime/io/UpdateAction.java
@@ -20,9 +20,10 @@
package org.apache.felix.sigil.common.runtime.io;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.io.OutputStream;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
@@ -51,9 +52,10 @@
}
- public UpdateAction( InputStream in, OutputStream out ) throws IOException
+ public UpdateAction( DataInputStream in, DataOutputStream out ) throws IOException
{
super( in, out );
+ // TODO Auto-generated constructor stub
}