You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jg...@apache.org on 2006/09/15 06:57:27 UTC
svn commit: r446519 [3/4] - in /geronimo/sandbox/gcache: ./ openwire/
openwire/src/ openwire/src/main/ openwire/src/main/java/
openwire/src/main/java/org/ openwire/src/main/java/org/apache/
openwire/src/main/java/org/apache/geronimo/ openwire/src/main/...
Added: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpBufferedOutputStream.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpBufferedOutputStream.java?view=auto&rev=446519
==============================================================================
--- geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpBufferedOutputStream.java (added)
+++ geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpBufferedOutputStream.java Thu Sep 14 21:57:24 2006
@@ -0,0 +1,142 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.geronimo.openwire.transport.tcp;
+
+import java.io.EOFException;
+import java.io.FilterOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+/**
+ * An optimized buffered outputstream for Tcp
+ *
+ * @version $Revision$
+ */
+
+public class TcpBufferedOutputStream extends FilterOutputStream {
+ private final static int BUFFER_SIZE = 8192;
+ private byte[] buffer;
+ private int count;
+ private boolean closed;
+
+ /**
+ * Constructor
+ *
+ * @param out
+ */
+ public TcpBufferedOutputStream(OutputStream out) {
+ this(out, BUFFER_SIZE);
+ }
+
+ /**
+ * Creates a new buffered output stream to write data to the specified underlying output stream with the specified
+ * buffer size.
+ *
+ * @param out the underlying output stream.
+ * @param size the buffer size.
+ * @throws IllegalArgumentException if size <= 0.
+ */
+ public TcpBufferedOutputStream(OutputStream out, int size) {
+ super(out);
+ if (size <= 0) {
+ throw new IllegalArgumentException("Buffer size <= 0");
+ }
+ buffer = new byte[size];
+ }
+
+ /**
+ * write a byte on to the stream
+ *
+ * @param b - byte to write
+ * @throws IOException
+ */
+ public void write(int b) throws IOException {
+ checkClosed();
+ if (availableBufferToWrite() < 1) {
+ flush();
+ }
+ buffer[count++] = (byte) b;
+ }
+
+
+ /**
+ * write a byte array to the stream
+ *
+ * @param b the byte buffer
+ * @param off the offset into the buffer
+ * @param len the length of data to write
+ * @throws IOException
+ */
+ public void write(byte b[], int off, int len) throws IOException {
+ checkClosed();
+ if (availableBufferToWrite() < len) {
+ flush();
+ }
+ if (buffer.length >= len) {
+ System.arraycopy(b, off, buffer, count, len);
+ count += len;
+ }
+ else {
+ out.write(b, off, len);
+ }
+ }
+
+ /**
+ * flush the data to the output stream
+ * This doesn't call flush on the underlying outputstream, because
+ * Tcp is particularly efficent at doing this itself ....
+ *
+ * @throws IOException
+ */
+ public void flush() throws IOException {
+ if (count > 0 && out != null) {
+ out.write(buffer, 0, count);
+ count = 0;
+ }
+ }
+
+ /**
+ * close this stream
+ *
+ * @throws IOException
+ */
+ public void close() throws IOException {
+ super.close();
+ closed = true;
+ }
+
+
+ /**
+ * Checks that the stream has not been closed
+ *
+ * @throws IOException
+ */
+ protected void checkClosed() throws IOException {
+ if (closed) {
+ throw new EOFException("Cannot write to the stream any more it has already been closed");
+ }
+ }
+
+ /**
+ * @return the amount free space in the buffer
+ */
+ private int availableBufferToWrite() {
+ return buffer.length - count;
+ }
+}
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpBufferedOutputStream.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpBufferedOutputStream.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpBufferedOutputStream.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpBufferedOutputStream.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransport.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransport.java?view=auto&rev=446519
==============================================================================
--- geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransport.java (added)
+++ geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransport.java Thu Sep 14 21:57:24 2006
@@ -0,0 +1,334 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.geronimo.openwire.transport.tcp;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.InterruptedIOException;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.net.SocketAddress;
+import java.net.SocketException;
+import java.net.SocketTimeoutException;
+import java.net.URI;
+import java.net.UnknownHostException;
+import java.util.Map;
+
+import javax.net.SocketFactory;
+
+import org.apache.activemq.Service;
+import org.apache.activemq.command.Command;
+import org.apache.activemq.transport.Transport;
+import org.apache.activemq.transport.TransportThreadSupport;
+import org.apache.activemq.util.IntrospectionSupport;
+import org.apache.activemq.util.ServiceStopper;
+import org.apache.activemq.wireformat.WireFormat;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * An implementation of the {@link Transport} interface using raw tcp/ip
+ *
+ * @version $Revision$
+ */
+public class TcpTransport extends TransportThreadSupport implements Transport, Service, Runnable {
+ private static final Log log = LogFactory.getLog(TcpTransport.class);
+
+ protected final URI remoteLocation;
+ protected final URI localLocation;
+ protected final WireFormat wireFormat;
+
+ protected int connectionTimeout = 30000;
+ protected int soTimeout = 0;
+ protected int socketBufferSize = 128 * 1024;
+ protected Socket socket;
+ protected DataOutputStream dataOut;
+ protected DataInputStream dataIn;
+ protected boolean trace;
+ protected boolean useLocalHost = true;
+ protected int minmumWireFormatVersion;
+ private Boolean keepAlive;
+ private Boolean tcpNoDelay;
+
+ /**
+ * Connect to a remote Node - e.g. a Broker
+ *
+ * @param wireFormat
+ * @param socketFactory
+ * @param remoteLocation
+ * @param localLocation -
+ * e.g. local InetAddress and local port
+ * @throws IOException
+ * @throws UnknownHostException
+ */
+ public TcpTransport(WireFormat wireFormat, SocketFactory socketFactory, URI remoteLocation, URI localLocation) throws UnknownHostException, IOException {
+ this.wireFormat = wireFormat;
+ this.socket = socketFactory.createSocket();
+ this.remoteLocation = remoteLocation;
+ this.localLocation = localLocation;
+ setDaemon(false);
+ }
+
+
+ /**
+ * Initialize from a server Socket
+ *
+ * @param wireFormat
+ * @param socket
+ * @throws IOException
+ */
+ public TcpTransport(WireFormat wireFormat, Socket socket) throws IOException {
+ this.wireFormat = wireFormat;
+ this.socket = socket;
+ this.remoteLocation = null;
+ this.localLocation = null;
+ setDaemon(true);
+ }
+
+ /**
+ * A one way asynchronous send
+ */
+ public void oneway(Command command) throws IOException {
+ checkStarted(command);
+ wireFormat.marshal(command, dataOut);
+ dataOut.flush();
+ }
+
+ /**
+ * @return pretty print of 'this'
+ */
+ public String toString() {
+ return "tcp://"+socket.getInetAddress()+":"+socket.getPort();
+ }
+
+ /**
+ * reads packets from a Socket
+ */
+ public void run() {
+ log.trace("TCP consumer thread starting");
+ while (!isStopped()) {
+ try {
+ Command command = (Command) wireFormat.unmarshal(dataIn);
+ doConsume(command);
+ }
+ catch (SocketTimeoutException e) {
+ }
+ catch (InterruptedIOException e) {
+ }
+ catch (IOException e) {
+ try {
+ stop();
+ }
+ catch (Exception e2) {
+ log.warn("Caught while closing: " + e2 + ". Now Closed", e2);
+ }
+ onException(e);
+ }
+ }
+ }
+
+ // Properties
+ // -------------------------------------------------------------------------
+
+ public boolean isTrace() {
+ return trace;
+ }
+
+ public void setTrace(boolean trace) {
+ this.trace = trace;
+ }
+
+ public int getMinmumWireFormatVersion() {
+ return minmumWireFormatVersion;
+ }
+
+ public void setMinmumWireFormatVersion(int minmumWireFormatVersion) {
+ this.minmumWireFormatVersion = minmumWireFormatVersion;
+ }
+
+ public boolean isUseLocalHost() {
+ return useLocalHost;
+ }
+
+ /**
+ * Sets whether 'localhost' or the actual local host name should be used to
+ * make local connections. On some operating systems such as Macs its not
+ * possible to connect as the local host name so localhost is better.
+ */
+ public void setUseLocalHost(boolean useLocalHost) {
+ this.useLocalHost = useLocalHost;
+ }
+
+ public int getSocketBufferSize() {
+ return socketBufferSize;
+ }
+
+ /**
+ * Sets the buffer size to use on the socket
+ */
+ public void setSocketBufferSize(int socketBufferSize) {
+ this.socketBufferSize = socketBufferSize;
+ }
+
+ public int getSoTimeout() {
+ return soTimeout;
+ }
+
+ /**
+ * Sets the socket timeout
+ */
+ public void setSoTimeout(int soTimeout) {
+ this.soTimeout = soTimeout;
+ }
+
+ public int getConnectionTimeout() {
+ return connectionTimeout;
+ }
+
+ /**
+ * Sets the timeout used to connect to the socket
+ */
+ public void setConnectionTimeout(int connectionTimeout) {
+ this.connectionTimeout = connectionTimeout;
+ }
+
+ public Boolean getKeepAlive() {
+ return keepAlive;
+ }
+
+ /**
+ * Enable/disable TCP KEEP_ALIVE mode
+ */
+ public void setKeepAlive(Boolean keepAlive) {
+ this.keepAlive = keepAlive;
+ }
+
+ public Boolean getTcpNoDelay() {
+ return tcpNoDelay;
+ }
+
+ /**
+ * Enable/disable the TCP_NODELAY option on the socket
+ */
+ public void setTcpNoDelay(Boolean tcpNoDelay) {
+ this.tcpNoDelay = tcpNoDelay;
+ }
+
+
+ // Implementation methods
+ // -------------------------------------------------------------------------
+ protected String resolveHostName(String host) throws UnknownHostException {
+ String localName = InetAddress.getLocalHost().getHostName();
+ if (localName != null && isUseLocalHost()) {
+ if (localName.equals(host)) {
+ return "localhost";
+ }
+ }
+ return host;
+ }
+
+ /**
+ * Configures the socket for use
+ *
+ * @param sock
+ * @throws SocketException
+ */
+ protected void initialiseSocket(Socket sock) throws SocketException {
+ try {
+ sock.setReceiveBufferSize(socketBufferSize);
+ sock.setSendBufferSize(socketBufferSize);
+ }
+ catch (SocketException se) {
+ log.warn("Cannot set socket buffer size = " + socketBufferSize);
+ log.debug("Cannot set socket buffer size. Reason: " + se, se);
+ }
+ sock.setSoTimeout(soTimeout);
+
+ if (keepAlive != null) {
+ sock.setKeepAlive(keepAlive.booleanValue());
+ }
+ if (tcpNoDelay != null) {
+ sock.setTcpNoDelay(tcpNoDelay.booleanValue());
+ }
+ }
+
+ protected void doStart() throws Exception {
+ connect();
+ super.doStart();
+ }
+
+ protected void connect() throws SocketException, IOException {
+
+ initialiseSocket(socket);
+
+ if( localLocation!=null ) {
+ SocketAddress localAddress = new InetSocketAddress(InetAddress.getByName(localLocation.getHost()), localLocation.getPort());
+ socket.bind(localAddress);
+ }
+ if( remoteLocation!=null ) {
+ String host = resolveHostName(remoteLocation.getHost());
+ InetSocketAddress remoteAddress = new InetSocketAddress(host, remoteLocation.getPort());
+ if (connectionTimeout >= 0) {
+ socket.connect(remoteAddress, connectionTimeout);
+ }
+ else {
+ socket.connect(remoteAddress);
+ }
+ }
+
+ initializeStreams();
+ }
+
+ protected void doStop(ServiceStopper stopper) throws Exception {
+ // Closing the streams flush the sockets before closing.. if the socket
+ // is hung.. then this hangs the close.
+ // closeStreams();
+ if (socket != null) {
+ socket.close();
+ }
+ }
+
+ protected void initializeStreams() throws IOException {
+ TcpBufferedInputStream buffIn = new TcpBufferedInputStream(socket.getInputStream(), 8*1024);
+ this.dataIn = new DataInputStream(buffIn);
+ TcpBufferedOutputStream buffOut = new TcpBufferedOutputStream(socket.getOutputStream(), 16*1024);
+ this.dataOut = new DataOutputStream(buffOut);
+ }
+
+ protected void closeStreams() throws IOException {
+ if (dataOut != null) {
+ dataOut.close();
+ }
+ if (dataIn != null) {
+ dataIn.close();
+ }
+ }
+
+ public void setSocketOptions(Map socketOptions) {
+ IntrospectionSupport.setProperties(socket, socketOptions);
+ }
+
+ public String getRemoteAddress() {
+ if(socket != null){
+ return "" + socket.getRemoteSocketAddress();
+ }
+ return null;
+ }
+}
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransport.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransport.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransport.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransport.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransportFactory.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransportFactory.java?view=auto&rev=446519
==============================================================================
--- geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransportFactory.java (added)
+++ geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransportFactory.java Thu Sep 14 21:57:24 2006
@@ -0,0 +1,141 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.geronimo.openwire.transport.tcp;
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.UnknownHostException;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.net.ServerSocketFactory;
+import javax.net.SocketFactory;
+
+import org.apache.activemq.openwire.OpenWireFormat;
+import org.apache.activemq.transport.InactivityMonitor;
+import org.apache.activemq.transport.Transport;
+import org.apache.activemq.transport.TransportFactory;
+import org.apache.activemq.transport.TransportLogger;
+import org.apache.activemq.transport.TransportServer;
+import org.apache.activemq.transport.WireFormatNegotiator;
+import org.apache.activemq.util.IOExceptionSupport;
+import org.apache.activemq.util.IntrospectionSupport;
+import org.apache.activemq.util.URISupport;
+import org.apache.activemq.wireformat.WireFormat;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class TcpTransportFactory extends TransportFactory {
+ private static final Log log = LogFactory.getLog(TcpTransportFactory.class);
+ public TransportServer doBind(String brokerId, final URI location) throws IOException {
+ try {
+ Map options = new HashMap(URISupport.parseParamters(location));
+
+ ServerSocketFactory serverSocketFactory = createServerSocketFactory();
+ TcpTransportServer server = createTcpTransportServer(location, serverSocketFactory);
+ server.setWireFormatFactory(createWireFormatFactory(options));
+ IntrospectionSupport.setProperties(server, options);
+ Map transportOptions = IntrospectionSupport.extractProperties(options, "transport.");
+ server.setTransportOption(transportOptions);
+ server.bind();
+
+ return server;
+ }
+ catch (URISyntaxException e) {
+ throw IOExceptionSupport.create(e);
+ }
+ }
+
+ /**
+ * Allows subclasses of TcpTransportFactory to create custom instances of TcpTransportServer.
+ *
+ * @param location
+ * @param serverSocketFactory
+ * @return
+ * @throws IOException
+ * @throws URISyntaxException
+ */
+ protected TcpTransportServer createTcpTransportServer(final URI location, ServerSocketFactory serverSocketFactory) throws IOException, URISyntaxException {
+ return new TcpTransportServer(this, location, serverSocketFactory);
+ }
+
+ public Transport compositeConfigure(Transport transport, WireFormat format, Map options) {
+
+ TcpTransport tcpTransport = (TcpTransport) transport.narrow(TcpTransport.class);
+ IntrospectionSupport.setProperties(tcpTransport, options);
+
+ Map socketOptions = IntrospectionSupport.extractProperties(options, "socket.");
+ tcpTransport.setSocketOptions(socketOptions);
+
+ if (tcpTransport.isTrace()) {
+ transport = new TransportLogger(transport);
+ }
+
+ transport = new InactivityMonitor(transport);
+
+ // Only need the WireFormatNegotiator if using openwire
+ if( format instanceof OpenWireFormat ) {
+ transport = new WireFormatNegotiator(transport, (OpenWireFormat)format, tcpTransport.getMinmumWireFormatVersion());
+ }
+
+ return transport;
+ }
+
+ protected Transport createTransport(URI location,WireFormat wf) throws UnknownHostException,IOException{
+ URI localLocation=null;
+ String path=location.getPath();
+ // see if the path is a local URI location
+ if (path != null && path.length() > 0) {
+ int localPortIndex = path.indexOf(':');
+ try {
+ Integer.parseInt(path.substring((localPortIndex + 1), path.length()));
+ String localString = location.getScheme() + ":/" + path;
+ localLocation = new URI(localString);
+ }
+ catch (Exception e) {
+ log.warn("path isn't a valid local location for TcpTransport to use", e);
+ }
+ }
+ SocketFactory socketFactory = createSocketFactory();
+ return createTcpTransport(wf, socketFactory, location, localLocation);
+ }
+
+ /**
+ * Allows subclasses of TcpTransportFactory to provide a create custom TcpTransport intances.
+ *
+ * @param location
+ * @param wf
+ * @param socketFactory
+ * @param localLocation
+ * @return
+ * @throws UnknownHostException
+ * @throws IOException
+ */
+ protected TcpTransport createTcpTransport(WireFormat wf, SocketFactory socketFactory, URI location, URI localLocation) throws UnknownHostException, IOException {
+ return new TcpTransport(wf, socketFactory, location, localLocation);
+ }
+
+ protected ServerSocketFactory createServerSocketFactory() {
+ return ServerSocketFactory.getDefault();
+ }
+
+ protected SocketFactory createSocketFactory() {
+ return SocketFactory.getDefault();
+ }
+}
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransportFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransportFactory.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransportFactory.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransportFactory.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransportServer.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransportServer.java?view=auto&rev=446519
==============================================================================
--- geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransportServer.java (added)
+++ geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransportServer.java Thu Sep 14 21:57:24 2006
@@ -0,0 +1,232 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.geronimo.openwire.transport.tcp;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.SocketTimeoutException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.UnknownHostException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.activemq.command.BrokerInfo;
+import org.apache.activemq.openwire.OpenWireFormatFactory;
+import org.apache.activemq.transport.Transport;
+import org.apache.activemq.transport.TransportServer;
+import org.apache.activemq.transport.TransportServerThreadSupport;
+import org.apache.activemq.util.IOExceptionSupport;
+import org.apache.activemq.util.ServiceStopper;
+import org.apache.activemq.wireformat.WireFormat;
+import org.apache.activemq.wireformat.WireFormatFactory;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import javax.net.ServerSocketFactory;
+
+/**
+ * A TCP based implementation of {@link TransportServer}
+ *
+ * @version $Revision$
+ */
+
+public class TcpTransportServer extends TransportServerThreadSupport {
+
+ private static final Log log = LogFactory.getLog(TcpTransportServer.class);
+ protected ServerSocket serverSocket;
+ protected int backlog = 5000;
+ protected WireFormatFactory wireFormatFactory = new OpenWireFormatFactory();
+ protected final TcpTransportFactory transportFactory;
+ protected long maxInactivityDuration = 30000;
+ protected int minmumWireFormatVersion;
+ protected boolean trace;
+ protected Map transportOptions;
+ protected final ServerSocketFactory serverSocketFactory;
+
+ public TcpTransportServer(TcpTransportFactory transportFactory, URI location, ServerSocketFactory serverSocketFactory) throws IOException, URISyntaxException {
+ super(location);
+ this.transportFactory=transportFactory;
+ this.serverSocketFactory = serverSocketFactory;
+ }
+
+ public void bind() throws IOException {
+ URI bind = getBindLocation();
+
+ String host = bind.getHost();
+ host = (host == null || host.length() == 0) ? "localhost" : host;
+ InetAddress addr = InetAddress.getByName(host);
+
+ try {
+ if (host.trim().equals("localhost") || addr.equals(InetAddress.getLocalHost())) {
+ this.serverSocket = serverSocketFactory.createServerSocket(bind.getPort(), backlog);
+ }
+ else {
+ this.serverSocket = serverSocketFactory.createServerSocket(bind.getPort(), backlog, addr);
+ }
+ this.serverSocket.setSoTimeout(2000);
+ }
+ catch (IOException e) {
+ throw IOExceptionSupport.create("Failed to bind to server socket: " + bind + " due to: " + e, e);
+ }
+ try {
+ setConnectURI(new URI(bind.getScheme(), bind.getUserInfo(), resolveHostName(bind.getHost()), serverSocket.getLocalPort(), bind.getPath(),
+ bind.getQuery(), bind.getFragment()));
+ } catch (URISyntaxException e) {
+ throw IOExceptionSupport.create(e);
+ }
+ }
+
+ /**
+ * @return Returns the wireFormatFactory.
+ */
+ public WireFormatFactory getWireFormatFactory() {
+ return wireFormatFactory;
+ }
+
+ /**
+ * @param wireFormatFactory
+ * The wireFormatFactory to set.
+ */
+ public void setWireFormatFactory(WireFormatFactory wireFormatFactory) {
+ this.wireFormatFactory = wireFormatFactory;
+ }
+
+ /**
+ * Associates a broker info with the transport server so that the transport
+ * can do discovery advertisements of the broker.
+ *
+ * @param brokerInfo
+ */
+ public void setBrokerInfo(BrokerInfo brokerInfo) {
+ }
+
+ public long getMaxInactivityDuration() {
+ return maxInactivityDuration;
+ }
+
+ public void setMaxInactivityDuration(long maxInactivityDuration) {
+ this.maxInactivityDuration = maxInactivityDuration;
+ }
+
+ public int getMinmumWireFormatVersion() {
+ return minmumWireFormatVersion;
+ }
+
+ public void setMinmumWireFormatVersion(int minmumWireFormatVersion) {
+ this.minmumWireFormatVersion = minmumWireFormatVersion;
+ }
+
+ public boolean isTrace() {
+ return trace;
+ }
+
+ public void setTrace(boolean trace) {
+ this.trace = trace;
+ }
+
+ /**
+ * pull Sockets from the ServerSocket
+ */
+ public void run() {
+ while (!isStopped()) {
+ Socket socket = null;
+ try {
+ socket = serverSocket.accept();
+ if (socket != null) {
+ if (isStopped() || getAcceptListener() == null) {
+ socket.close();
+ }
+ else {
+ HashMap options = new HashMap();
+ options.put("maxInactivityDuration", new Long(maxInactivityDuration));
+ options.put("minmumWireFormatVersion", new Integer(minmumWireFormatVersion));
+ options.put("trace", new Boolean(trace));
+ options.putAll(transportOptions);
+ WireFormat format = wireFormatFactory.createWireFormat();
+ Transport transport = createTransport(socket, format);
+ Transport configuredTransport = transportFactory.configure(transport, format, options);
+ getAcceptListener().onAccept(configuredTransport);
+ }
+ }
+ }
+ catch (SocketTimeoutException ste) {
+ // expect this to happen
+ }
+ catch (Exception e) {
+ if (!isStopping()) {
+ onAcceptError(e);
+ } else if (!isStopped()) {
+ log.warn("run()", e);
+ onAcceptError(e);
+ }
+ }
+ }
+ }
+
+ /**
+ * Allow derived classes to override the Transport implementation that this transport server creates.
+ * @param socket
+ * @param format
+ * @return
+ * @throws IOException
+ */
+ protected Transport createTransport(Socket socket, WireFormat format) throws IOException {
+ return new TcpTransport(format, socket);
+ }
+
+ /**
+ * @return pretty print of this
+ */
+ public String toString() {
+ return ""+getBindLocation();
+ }
+
+ /**
+ *
+ * @param hostName
+ * @return real hostName
+ * @throws UnknownHostException
+ */
+ protected String resolveHostName(String hostName) throws UnknownHostException {
+ String result = hostName;
+ // hostname can be null for vm:// protocol ...
+ if (hostName != null && (hostName.equalsIgnoreCase("localhost") || hostName.equals("127.0.0.1"))) {
+ result = InetAddress.getLocalHost().getHostName();
+ }
+ return result;
+ }
+
+ protected void doStop(ServiceStopper stopper) throws Exception {
+ super.doStop(stopper);
+ if (serverSocket != null) {
+ serverSocket.close();
+ }
+ }
+
+ public InetSocketAddress getSocketAddress() {
+ return (InetSocketAddress)serverSocket.getLocalSocketAddress();
+ }
+
+ public void setTransportOption(Map transportOptions) {
+ this.transportOptions = transportOptions;
+ }
+}
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransportServer.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransportServer.java
------------------------------------------------------------------------------
svn:executable = *
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransportServer.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/transport/tcp/TcpTransportServer.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ByteArrayInputStream.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ByteArrayInputStream.java?view=auto&rev=446519
==============================================================================
--- geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ByteArrayInputStream.java (added)
+++ geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ByteArrayInputStream.java Thu Sep 14 21:57:24 2006
@@ -0,0 +1,93 @@
+package org.apache.geronimo.openwire.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Copyright 2006 The Apache Software Foundation
+ * <p/>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+public class ByteArrayInputStream extends InputStream {
+ byte buffer[];
+ int limit;
+ int pos;
+ int mark;
+
+ public ByteArrayInputStream(byte data[]) {
+ this(data, 0, data.length);
+ }
+
+ public ByteArrayInputStream(ByteSequence sequence) {
+ this(sequence.getData(), sequence.getOffset(), sequence.getLength());
+ }
+
+ public ByteArrayInputStream(byte data[], int offset, int size) {
+ this.buffer = data;
+ this.mark= this.pos = offset;
+ this.limit = offset+size;
+ }
+
+ public int read() throws IOException {
+ if( pos < limit )
+ return buffer[pos++] & 0xff;
+ else
+ return -1;
+ }
+
+ public int read(byte[] b) throws IOException {
+ return read(b, 0, b.length);
+ }
+
+ public int read(byte b[], int off, int len) {
+ if (pos < limit) {
+ len = Math.min(len, limit-pos);
+ if (len > 0) {
+ System.arraycopy(buffer, pos, b, off, len);
+ pos += len;
+ }
+ return len;
+ } else {
+ return -1;
+ }
+ }
+
+ public long skip(long len) throws IOException {
+ if (pos < limit) {
+ len = Math.min(len, limit-pos);
+ if (len > 0) {
+ pos += len;
+ }
+ return len;
+ } else {
+ return -1;
+ }
+ }
+
+ public int available() {
+ return limit - pos;
+ }
+
+ public boolean markSupported() {
+ return true;
+ }
+
+ public void mark(int markpos) {
+ mark = pos;
+ }
+
+ public void reset() {
+ pos = mark;
+ }
+
+}
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ByteArrayInputStream.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ByteArrayInputStream.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ByteArrayInputStream.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ByteArrayOutputStream.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ByteArrayOutputStream.java?view=auto&rev=446519
==============================================================================
--- geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ByteArrayOutputStream.java (added)
+++ geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ByteArrayOutputStream.java Thu Sep 14 21:57:24 2006
@@ -0,0 +1,84 @@
+package org.apache.geronimo.openwire.util;
+
+import java.io.OutputStream;
+
+/**
+ * Copyright 2006 The Apache Software Foundation
+ * <p/>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Very similar to the java.io.ByteArrayOutputStream but this version
+ * is not thread safe and the resulting data is returned in a ByteSequence
+ * to avoid an extra byte[] allocation.
+ */
+public class ByteArrayOutputStream extends OutputStream {
+
+ byte buffer[];
+ int size;
+
+ public ByteArrayOutputStream() {
+ this(512);
+ }
+
+ public ByteArrayOutputStream(int capacity) {
+ buffer = new byte[capacity];
+ }
+
+ public void write(int b) {
+ int newsize = size + 1;
+ checkCapacity(newsize);
+ buffer[size] = (byte) b;
+ size = newsize;
+ }
+
+ public void write(byte b[], int off, int len) {
+ int newsize = size + len;
+ checkCapacity(newsize);
+ System.arraycopy(b, off, buffer, size, len);
+ size = newsize;
+ }
+
+ /**
+ * Ensures the the buffer has at least the minimumCapacity specified.
+ *
+ * @param minimumCapacity
+ */
+ private void checkCapacity(int minimumCapacity) {
+ if (minimumCapacity > buffer.length) {
+ byte b[] = new byte[Math.max(buffer.length << 1, minimumCapacity)];
+ System.arraycopy(buffer, 0, b, 0, size);
+ buffer = b;
+ }
+ }
+
+ public void reset() {
+ size = 0;
+ }
+
+ public ByteSequence toByteSequence() {
+ return new ByteSequence(buffer, 0, size);
+ }
+
+ public byte[] toByteArray() {
+ byte rc[] = new byte[size];
+ System.arraycopy(buffer, 0, rc, 0, size);
+ return rc;
+ }
+
+ public int size() {
+ return size;
+ }
+}
+
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ByteArrayOutputStream.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ByteArrayOutputStream.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ByteArrayOutputStream.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ByteSequence.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ByteSequence.java?view=auto&rev=446519
==============================================================================
--- geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ByteSequence.java (added)
+++ geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ByteSequence.java Thu Sep 14 21:57:24 2006
@@ -0,0 +1,58 @@
+package org.apache.geronimo.openwire.util;
+
+/**
+ * Copyright 2006 The Apache Software Foundation
+ * <p/>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+public class ByteSequence {
+ public byte[] data;
+ public int offset;
+ public int length;
+
+ public ByteSequence(byte data[]) {
+ this.data = data;
+ this.offset = 0;
+ this.length = data.length;
+ }
+
+ public ByteSequence(byte data[], int offset, int length) {
+ this.data = data;
+ this.offset = offset;
+ this.length = length;
+ }
+
+ public byte[] getData() {
+ return data;
+ }
+
+ public int getLength() {
+ return length;
+ }
+
+ public int getOffset() {
+ return offset;
+ }
+
+ public void setData(byte[] data) {
+ this.data = data;
+ }
+
+ public void setLength(int length) {
+ this.length = length;
+ }
+
+ public void setOffset(int offset) {
+ this.offset = offset;
+ }
+}
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ByteSequence.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ByteSequence.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ByteSequence.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/Callback.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/Callback.java?view=auto&rev=446519
==============================================================================
--- geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/Callback.java (added)
+++ geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/Callback.java Thu Sep 14 21:57:24 2006
@@ -0,0 +1,26 @@
+package org.apache.geronimo.openwire.util;
+
+/**
+ * Copyright 2006 The Apache Software Foundation
+ * <p/>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+public interface Callback {
+ /**
+ * Executes some piece of code within a transaction
+ * performing a commit if there is no exception thrown
+ * else a rollback is performed
+ * @throws Exception TODO
+ */
+ public void execute() throws Exception;
+}
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/Callback.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/Callback.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/Callback.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ClassLoading.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ClassLoading.java?view=auto&rev=446519
==============================================================================
--- geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ClassLoading.java (added)
+++ geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ClassLoading.java Thu Sep 14 21:57:24 2006
@@ -0,0 +1,239 @@
+package org.apache.geronimo.openwire.util;
+
+import java.lang.reflect.Array;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Copyright 2006 The Apache Software Foundation
+ * <p/>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/**
+ * Utilities for loading classes.
+ *
+ * @version $Rev$ $Date$
+ */
+public class ClassLoading {
+
+ /**
+ * Load a class for the given name. <p/>
+ * <p>
+ * Handles loading primitive types as well as VM class and array syntax.
+ *
+ * @param className
+ * The name of the Class to be loaded.
+ * @param classLoader
+ * The class loader to load the Class object from.
+ * @return The Class object for the given name.
+ * @throws ClassNotFoundException
+ * Failed to load Class object.
+ */
+ public static Class loadClass(final String className, final ClassLoader classLoader) throws ClassNotFoundException {
+ if (className == null) {
+ throw new IllegalArgumentException("className is null");
+ }
+
+ // First just try to load
+ try {
+ return load(className, classLoader);
+ } catch (ClassNotFoundException ignore) {
+ // handle special cases below
+ }
+
+ Class type = null;
+
+ // Check if it is a primitive type
+ type = getPrimitiveType(className);
+ if (type != null)
+ return type;
+
+ // Check if it is a vm primitive
+ type = getVMPrimitiveType(className);
+ if (type != null)
+ return type;
+
+ // Handle VM class syntax (Lclassname;)
+ if (className.charAt(0) == 'L' && className.charAt(className.length() - 1) == ';') {
+ String name = className.substring(1, className.length() - 1);
+ return load(name, classLoader);
+ }
+
+ // Handle VM array syntax ([type)
+ if (className.charAt(0) == '[') {
+ int arrayDimension = className.lastIndexOf('[') + 1;
+ String componentClassName = className.substring(arrayDimension, className.length());
+ type = loadClass(componentClassName, classLoader);
+
+ int dim[] = new int[arrayDimension];
+ java.util.Arrays.fill(dim, 0);
+ return Array.newInstance(type, dim).getClass();
+ }
+
+ // Handle user friendly type[] syntax
+ if (className.endsWith("[]")) {
+ // get the base component class name and the arrayDimensions
+ int arrayDimension = 0;
+ String componentClassName = className;
+ while (componentClassName.endsWith("[]")) {
+ componentClassName = componentClassName.substring(0, componentClassName.length() - 2);
+ arrayDimension++;
+ }
+
+ // load the base type
+ type = loadClass(componentClassName, classLoader);
+
+ // return the array type
+ int[] dim = new int[arrayDimension];
+ java.util.Arrays.fill(dim, 0);
+ return Array.newInstance(type, dim).getClass();
+ }
+
+ // Else we can not load (give up)
+ throw new ClassNotFoundException(className);
+ }
+
+ private static Class load(final String className, final ClassLoader classLoader) throws ClassNotFoundException {
+ if (classLoader == null)
+ return Class.forName(className);
+ else
+ return classLoader.loadClass(className);
+ }
+
+ public static String getClassName(Class clazz) {
+ StringBuffer rc = new StringBuffer();
+ while (clazz.isArray()) {
+ rc.append('[');
+ clazz = clazz.getComponentType();
+ }
+ if (!clazz.isPrimitive()) {
+ rc.append('L');
+ rc.append(clazz.getName());
+ rc.append(';');
+ } else {
+ rc.append(VM_PRIMITIVES_REVERSE.get(clazz));
+ }
+ return rc.toString();
+ }
+
+ /**
+ * Primitive type name -> class map.
+ */
+ private static final Map PRIMITIVES = new HashMap();
+
+ /** Setup the primitives map. */
+ static {
+ PRIMITIVES.put("boolean", Boolean.TYPE);
+ PRIMITIVES.put("byte", Byte.TYPE);
+ PRIMITIVES.put("char", Character.TYPE);
+ PRIMITIVES.put("short", Short.TYPE);
+ PRIMITIVES.put("int", Integer.TYPE);
+ PRIMITIVES.put("long", Long.TYPE);
+ PRIMITIVES.put("float", Float.TYPE);
+ PRIMITIVES.put("double", Double.TYPE);
+ PRIMITIVES.put("void", Void.TYPE);
+ }
+
+ /**
+ * Get the primitive type for the given primitive name.
+ *
+ * @param name
+ * Primitive type name (boolean, byte, int, ...)
+ * @return Primitive type or null.
+ */
+ private static Class getPrimitiveType(final String name) {
+ return (Class) PRIMITIVES.get(name);
+ }
+
+ /**
+ * VM primitive type name -> primitive type
+ */
+ private static final HashMap VM_PRIMITIVES = new HashMap();
+
+ /** Setup the vm primitives map. */
+ static {
+ VM_PRIMITIVES.put("B", byte.class);
+ VM_PRIMITIVES.put("C", char.class);
+ VM_PRIMITIVES.put("D", double.class);
+ VM_PRIMITIVES.put("F", float.class);
+ VM_PRIMITIVES.put("I", int.class);
+ VM_PRIMITIVES.put("J", long.class);
+ VM_PRIMITIVES.put("S", short.class);
+ VM_PRIMITIVES.put("Z", boolean.class);
+ VM_PRIMITIVES.put("V", void.class);
+ }
+
+ /**
+ * VM primitive type primitive type -> name
+ */
+ private static final HashMap VM_PRIMITIVES_REVERSE = new HashMap();
+
+ /** Setup the vm primitives reverse map. */
+ static {
+ VM_PRIMITIVES_REVERSE.put(byte.class, "B");
+ VM_PRIMITIVES_REVERSE.put(char.class, "C");
+ VM_PRIMITIVES_REVERSE.put(double.class, "D");
+ VM_PRIMITIVES_REVERSE.put(float.class, "F");
+ VM_PRIMITIVES_REVERSE.put(int.class, "I");
+ VM_PRIMITIVES_REVERSE.put(long.class, "J");
+ VM_PRIMITIVES_REVERSE.put(short.class, "S");
+ VM_PRIMITIVES_REVERSE.put(boolean.class, "Z");
+ VM_PRIMITIVES_REVERSE.put(void.class, "V");
+ }
+
+ /**
+ * Get the primitive type for the given VM primitive name. <p/>
+ * <p>
+ * Mapping:
+ *
+ * <pre>
+ *
+ * B - byte
+ * C - char
+ * D - double
+ * F - float
+ * I - int
+ * J - long
+ * S - short
+ * Z - boolean
+ * V - void
+ *
+ * </pre>
+ *
+ * @param name
+ * VM primitive type name (B, C, J, ...)
+ * @return Primitive type or null.
+ */
+ private static Class getVMPrimitiveType(final String name) {
+ return (Class) VM_PRIMITIVES.get(name);
+ }
+
+ /**
+ * Map of primitive types to their wrapper classes
+ */
+ private static final Map PRIMITIVE_WRAPPERS = new HashMap();
+
+ /** Setup the wrapper map. */
+ static {
+ PRIMITIVE_WRAPPERS.put(Boolean.TYPE, Boolean.class);
+ PRIMITIVE_WRAPPERS.put(Byte.TYPE, Byte.class);
+ PRIMITIVE_WRAPPERS.put(Character.TYPE, Character.class);
+ PRIMITIVE_WRAPPERS.put(Double.TYPE, Double.class);
+ PRIMITIVE_WRAPPERS.put(Float.TYPE, Float.class);
+ PRIMITIVE_WRAPPERS.put(Integer.TYPE, Integer.class);
+ PRIMITIVE_WRAPPERS.put(Long.TYPE, Long.class);
+ PRIMITIVE_WRAPPERS.put(Short.TYPE, Short.class);
+ PRIMITIVE_WRAPPERS.put(Void.TYPE, Void.class);
+ }
+}
+
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ClassLoading.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ClassLoading.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ClassLoading.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ClassLoadingAwareObjectInputStream.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ClassLoadingAwareObjectInputStream.java?view=auto&rev=446519
==============================================================================
--- geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ClassLoadingAwareObjectInputStream.java (added)
+++ geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ClassLoadingAwareObjectInputStream.java Thu Sep 14 21:57:24 2006
@@ -0,0 +1,58 @@
+package org.apache.geronimo.openwire.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectStreamClass;
+import java.lang.reflect.Proxy;
+
+/**
+ * Copyright 2006 The Apache Software Foundation
+ * <p/>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+public class ClassLoadingAwareObjectInputStream extends ObjectInputStream {
+
+ private static final ClassLoader FALLBACK_CLASS_LOADER = ClassLoadingAwareObjectInputStream.class.getClassLoader();
+
+ public ClassLoadingAwareObjectInputStream(InputStream in) throws IOException {
+ super(in);
+ }
+
+ protected Class resolveClass(ObjectStreamClass classDesc) throws IOException, ClassNotFoundException {
+ ClassLoader cl = Thread.currentThread().getContextClassLoader();
+ return load(classDesc.getName(), cl);
+ }
+
+ protected Class resolveProxyClass(String[] interfaces) throws IOException, ClassNotFoundException {
+ ClassLoader cl = Thread.currentThread().getContextClassLoader();
+ Class[] cinterfaces = new Class[interfaces.length];
+ for (int i = 0; i < interfaces.length; i++)
+ cinterfaces[i] = load(interfaces[i], cl);
+
+ try {
+ return Proxy.getProxyClass(cinterfaces[0].getClassLoader(), cinterfaces);
+ } catch (IllegalArgumentException e) {
+ throw new ClassNotFoundException(null, e);
+ }
+ }
+
+ private Class load(String className, ClassLoader cl) throws ClassNotFoundException {
+ try {
+ return ClassLoading.loadClass(className, cl);
+ } catch ( ClassNotFoundException e ) {
+ return ClassLoading.loadClass(className, FALLBACK_CLASS_LOADER);
+ }
+ }
+
+}
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ClassLoadingAwareObjectInputStream.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ClassLoadingAwareObjectInputStream.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/ClassLoadingAwareObjectInputStream.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/FactoryFinder.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/FactoryFinder.java?view=auto&rev=446519
==============================================================================
--- geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/FactoryFinder.java (added)
+++ geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/FactoryFinder.java Thu Sep 14 21:57:24 2006
@@ -0,0 +1,103 @@
+package org.apache.geronimo.openwire.util;
+
+import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
+
+import java.io.IOException;
+import java.io.BufferedInputStream;
+import java.io.InputStream;
+import java.util.Properties;
+
+/**
+ * Copyright 2006 The Apache Software Foundation
+ * <p/>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+public class FactoryFinder {
+
+ private final String path;
+ private final ConcurrentHashMap classMap = new ConcurrentHashMap();
+
+ public FactoryFinder(String path) {
+ this.path = path;
+ }
+
+ /**
+ * Creates a new instance of the given key
+ *
+ * @param key is the key to add to the path to find a text file
+ * containing the factory name
+ * @return a newly created instance
+ */
+ public Object newInstance(String key)
+ throws IllegalAccessException, InstantiationException, IOException, ClassNotFoundException
+ {
+ return newInstance(key, null);
+ }
+
+ public Object newInstance(String key, String propertyPrefix)
+ throws IllegalAccessException, InstantiationException, IOException, ClassNotFoundException
+ {
+ if (propertyPrefix == null)
+ propertyPrefix = "";
+
+ Class clazz = (Class) classMap.get(propertyPrefix + key);
+ if (clazz == null) {
+ clazz = newInstance(doFindFactoryProperies(key), propertyPrefix);
+ classMap.put(propertyPrefix + key, clazz);
+ }
+ return clazz.newInstance();
+ }
+
+ private Class newInstance(Properties properties, String propertyPrefix) throws ClassNotFoundException, IOException {
+
+ String className = properties.getProperty(propertyPrefix + "class");
+ if (className == null) {
+ throw new IOException("Expected property is missing: " + propertyPrefix + "class");
+ }
+ Class clazz;
+ try {
+ clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
+ } catch (ClassNotFoundException e) {
+ clazz = FactoryFinder.class.getClassLoader().loadClass(className);
+ }
+
+ return clazz;
+ }
+
+ private Properties doFindFactoryProperies(String key) throws IOException {
+ String uri = path + key;
+
+ // lets try the thread context class loader first
+ InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(uri);
+ if (in == null) {
+ in = FactoryFinder.class.getClassLoader().getResourceAsStream(uri);
+ if (in == null) {
+ throw new IOException("Could not find factory class for resource: " + uri);
+ }
+ }
+
+ // lets load the file
+ BufferedInputStream reader = null;
+ try {
+ reader = new BufferedInputStream(in);
+ Properties properties = new Properties();
+ properties.load(reader);
+ return properties;
+ } finally {
+ try {
+ reader.close();
+ } catch (Exception e) {
+ }
+ }
+ }
+}
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/FactoryFinder.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/FactoryFinder.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/FactoryFinder.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/IOExceptionSupport.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/IOExceptionSupport.java?view=auto&rev=446519
==============================================================================
--- geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/IOExceptionSupport.java (added)
+++ geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/IOExceptionSupport.java Thu Sep 14 21:57:24 2006
@@ -0,0 +1,47 @@
+package org.apache.geronimo.openwire.util;
+
+import java.io.IOException;
+
+/**
+ * Copyright 2006 The Apache Software Foundation
+ * <p/>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+final public class IOExceptionSupport {
+
+ public static IOException create(String msg, Throwable cause) {
+ IOException exception = new IOException(msg);
+ exception.initCause(cause);
+ return exception;
+ }
+
+ public static IOException create(String msg, Exception cause) {
+ IOException exception = new IOException(msg);
+ exception.initCause(cause);
+ return exception;
+ }
+
+ public static IOException create(Throwable cause) {
+ IOException exception = new IOException(cause.getMessage());
+ exception.initCause(cause);
+ return exception;
+ }
+
+ public static IOException create(Exception cause) {
+ IOException exception = new IOException(cause.getMessage());
+ exception.initCause(cause);
+ return exception;
+ }
+
+}
+
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/IOExceptionSupport.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/IOExceptionSupport.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/IOExceptionSupport.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/IntSequenceGenerator.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/IntSequenceGenerator.java?view=auto&rev=446519
==============================================================================
--- geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/IntSequenceGenerator.java (added)
+++ geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/IntSequenceGenerator.java Thu Sep 14 21:57:24 2006
@@ -0,0 +1,32 @@
+package org.apache.geronimo.openwire.util;
+
+/**
+ * Copyright 2006 The Apache Software Foundation
+ * <p/>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+public class IntSequenceGenerator {
+ private int lastSequenceId;
+
+ public synchronized int getNextSequenceId() {
+ return ++lastSequenceId;
+ }
+
+ public synchronized int getLastSequenceId() {
+ return lastSequenceId;
+ }
+
+ public synchronized void setLastSequenceId(int l) {
+ lastSequenceId = l;
+ }
+}
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/IntSequenceGenerator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/IntSequenceGenerator.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/IntSequenceGenerator.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/IntrospectionSupport.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/IntrospectionSupport.java?view=auto&rev=446519
==============================================================================
--- geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/IntrospectionSupport.java (added)
+++ geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/IntrospectionSupport.java Thu Sep 14 21:57:24 2006
@@ -0,0 +1,286 @@
+package org.apache.geronimo.openwire.util;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.Map.Entry;
+import java.util.*;
+import java.net.URISyntaxException;
+import java.net.URI;
+import java.beans.PropertyEditor;
+import java.beans.PropertyEditorManager;
+
+/**
+ * Copyright 2006 The Apache Software Foundation
+ * <p/>
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+public class IntrospectionSupport {
+
+
+ static public boolean getProperties(Object target, Map props, String optionPrefix) {
+
+ boolean rc = false;
+ if( target == null )
+ throw new IllegalArgumentException("target was null.");
+ if( props == null )
+ throw new IllegalArgumentException("props was null.");
+
+ if( optionPrefix == null )
+ optionPrefix="";
+
+ Class clazz = target.getClass();
+ Method[] methods = clazz.getMethods();
+ for (int i = 0; i < methods.length; i++) {
+ Method method = methods[i];
+ String name = method.getName();
+ Class type = method.getReturnType();
+ Class params[] = method.getParameterTypes();
+ if( name.startsWith("get") && params.length==0 &&
+ type!=null && isSettableType(type)) {
+
+ try {
+
+ Object value = method.invoke(target, new Object[]{});
+ if( value == null )
+ continue;
+
+ String strValue = convertToString(value, type);
+ if( strValue ==null )
+ continue;
+
+ name = name.substring(3,4).toLowerCase()+name.substring(4);
+ props.put(optionPrefix+name, strValue);
+ rc = true;
+
+ } catch ( Throwable ignore) {
+ }
+
+ }
+ }
+
+ return rc;
+ }
+
+
+
+ static public boolean setProperties(Object target, Map props, String optionPrefix) {
+ boolean rc = false;
+ if( target == null )
+ throw new IllegalArgumentException("target was null.");
+ if( props == null )
+ throw new IllegalArgumentException("props was null.");
+
+ for (Iterator iter = props.keySet().iterator(); iter.hasNext();) {
+ String name = (String) iter.next();
+ if( name.startsWith(optionPrefix) ) {
+ Object value = props.get(name);
+ name = name.substring(optionPrefix.length());
+ if( setProperty(target, name, value) ) {
+ iter.remove();
+ rc = true;
+ }
+ }
+ }
+ return rc;
+ }
+
+ public static Map extractProperties(Map props, String optionPrefix) {
+ if( props == null )
+ throw new IllegalArgumentException("props was null.");
+
+ HashMap rc = new HashMap(props.size());
+
+ for (Iterator iter = props.keySet().iterator(); iter.hasNext();) {
+ String name = (String) iter.next();
+ if( name.startsWith(optionPrefix) ) {
+ Object value = props.get(name);
+ name = name.substring(optionPrefix.length());
+ rc.put(name, value);
+ iter.remove();
+ }
+ }
+
+ return rc;
+ }
+
+ public static boolean setProperties(Object target, Map props) {
+ boolean rc = false;
+
+ if( target == null )
+ throw new IllegalArgumentException("target was null.");
+ if( props == null )
+ throw new IllegalArgumentException("props was null.");
+
+ for (Iterator iter = props.entrySet().iterator(); iter.hasNext();) {
+ Map.Entry entry = (Entry) iter.next();
+ if( setProperty(target, (String) entry.getKey(), entry.getValue()) ) {
+ iter.remove();
+ rc=true;
+ }
+ }
+
+ return rc;
+ }
+
+ private static boolean setProperty(Object target, String name, Object value) {
+ try {
+ Class clazz = target.getClass();
+ Method setter = findSetterMethod(clazz, name);
+ if( setter == null )
+ return false;
+
+ // If the type is null or it matches the needed type, just use the value directly
+ if( value == null || value.getClass()==setter.getParameterTypes()[0] ) {
+ setter.invoke(target, new Object[]{value});
+ } else {
+ // We need to convert it
+ setter.invoke(target, new Object[]{ convert(value, setter.getParameterTypes()[0]) });
+ }
+ return true;
+ } catch (Throwable ignore) {
+ return false;
+ }
+ }
+
+ private static Object convert(Object value, Class type) throws URISyntaxException {
+ PropertyEditor editor = PropertyEditorManager.findEditor(type);
+ if( editor != null ) {
+ editor.setAsText(value.toString());
+ return editor.getValue();
+ }
+ if( type == URI.class ) {
+ return new URI(value.toString());
+ }
+ return null;
+ }
+
+ private static String convertToString(Object value, Class type) throws URISyntaxException {
+ PropertyEditor editor = PropertyEditorManager.findEditor(type);
+ if( editor != null ) {
+ editor.setValue(value);
+ return editor.getAsText();
+ }
+ if( type == URI.class ) {
+ return ((URI)value).toString();
+ }
+ return null;
+ }
+
+ private static Method findSetterMethod(Class clazz, String name) {
+ // Build the method name.
+ name = "set"+name.substring(0,1).toUpperCase()+name.substring(1);
+ Method[] methods = clazz.getMethods();
+ for (int i = 0; i < methods.length; i++) {
+ Method method = methods[i];
+ Class params[] = method.getParameterTypes();
+ if( method.getName().equals(name)
+ && params.length==1
+ && isSettableType(params[0])) {
+ return method;
+ }
+ }
+ return null;
+ }
+
+ private static boolean isSettableType(Class clazz) {
+ if( PropertyEditorManager.findEditor(clazz)!=null )
+ return true;
+ if( clazz == URI.class )
+ return true;
+ if( clazz == Boolean.class )
+ return true;
+ return false;
+ }
+
+ static public String toString(Object target) {
+ return toString(target, Object.class);
+ }
+
+ static public String toString(Object target, Class stopClass) {
+ LinkedHashMap map = new LinkedHashMap();
+ addFields(target, target.getClass(), stopClass, map);
+ StringBuffer buffer = new StringBuffer(simpleName(target.getClass()));
+ buffer.append(" {");
+ Set entrySet = map.entrySet();
+ boolean first = true;
+ for (Iterator iter = entrySet.iterator(); iter.hasNext();) {
+ Map.Entry entry = (Map.Entry) iter.next();
+ if (first) {
+ first = false;
+ }
+ else {
+ buffer.append(", ");
+ }
+ buffer.append(entry.getKey());
+ buffer.append(" = ");
+ appendToString(buffer, entry.getValue());
+ }
+ buffer.append("}");
+ return buffer.toString();
+ }
+
+ protected static void appendToString(StringBuffer buffer, Object value) {
+ if (value instanceof ActiveMQDestination) {
+ ActiveMQDestination destination = (ActiveMQDestination) value;
+ buffer.append(destination.getQualifiedName());
+ }
+ else {
+ buffer.append(value);
+ }
+ }
+
+ static public String simpleName(Class clazz) {
+ String name = clazz.getName();
+ int p = name.lastIndexOf(".");
+ if( p >= 0 ) {
+ name = name.substring(p+1);
+ }
+ return name;
+ }
+
+
+ static private void addFields(Object target, Class startClass, Class stopClass, LinkedHashMap map) {
+
+ if( startClass!=stopClass )
+ addFields( target, startClass.getSuperclass(), stopClass, map );
+
+ Field[] fields = startClass.getDeclaredFields();
+ for (int i = 0; i < fields.length; i++) {
+ Field field = fields[i];
+ if( Modifier.isStatic(field.getModifiers()) ||
+ Modifier.isTransient(field.getModifiers()) ||
+ Modifier.isPrivate(field.getModifiers()) ) {
+ continue;
+ }
+
+ try {
+ field.setAccessible(true);
+ Object o = field.get(target);
+ if( o!=null && o.getClass().isArray() ) {
+ try {
+ o = Arrays.asList((Object[]) o);
+ } catch (Throwable e) {
+ }
+ }
+ map.put(field.getName(), o);
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
+ }
+
+ }
+
+
+}
+
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/IntrospectionSupport.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/IntrospectionSupport.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/geronimo/openwire/util/IntrospectionSupport.java
------------------------------------------------------------------------------
svn:mime-type = text/plain