Added: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/
geronimo/openwire/util/MarshallingSupport.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/openwire/
src/main/java/org/apache/geronimo/openwire/util/
MarshallingSupport.java?view=auto&rev=446519
======================================================================
========
--- geronimo/sandbox/gcache/openwire/src/main/java/org/apache/
geronimo/openwire/util/MarshallingSupport.java (added)
+++ geronimo/sandbox/gcache/openwire/src/main/java/org/apache/
geronimo/openwire/util/MarshallingSupport.java Thu Sep 14 21:57:24
2006
@@ -0,0 +1,309 @@
+package org.apache.geronimo.openwire.util;
+
+import java.io.*;
+import java.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 MarshallingSupport {
+
+ public static final byte NULL = 0;
+ public static final byte BOOLEAN_TYPE = 1;
+ public static final byte BYTE_TYPE = 2;
+ public static final byte CHAR_TYPE = 3;
+ public static final byte SHORT_TYPE = 4;
+ public static final byte INTEGER_TYPE = 5;
+ public static final byte LONG_TYPE = 6;
+ public static final byte DOUBLE_TYPE = 7;
+ public static final byte FLOAT_TYPE = 8;
+ public static final byte STRING_TYPE = 9;
+ public static final byte BYTE_ARRAY_TYPE = 10;
+ public static final byte MAP_TYPE = 11;
+ public static final byte LIST_TYPE = 12;
+ public static final byte BIG_STRING_TYPE = 13;
+
+ static public void marshalPrimitiveMap(Map map,
DataOutputStream out) throws IOException {
+ if( map == null ) {
+ out.writeInt(-1);
+ } else {
+ out.writeInt(map.size());
+ for (Iterator iter = map.keySet().iterator();
iter.hasNext();) {
+ String name = (String) iter.next();
+ out.writeUTF(name);
+ Object value = map.get(name);
+ marshalPrimitive(out, value);
+ }
+ }
+ }
+
+ static public Map unmarshalPrimitiveMap(DataInputStream in)
throws IOException {
+ return unmarshalPrimitiveMap(in, Integer.MAX_VALUE);
+ }
+
+ /**
+ * @param in
+ * @return
+ * @throws IOException
+ * @throws IOException
+ */
+ public static Map unmarshalPrimitiveMap(DataInputStream in,
int max_property_size) throws IOException {
+ int size = in.readInt();
+ if( size > max_property_size ) {
+ throw new IOException("Primitive map is larger than
the allowed size: "+size);
+ }
+ if( size < 0 ) {
+ return null;
+ } else {
+ HashMap rc = new HashMap(size);
+ for(int i=0; i < size; i++) {
+ String name = in.readUTF();
+ rc.put(name, unmarshalPrimitive(in));
+ }
+ return rc;
+ }
+
+ }
+
+ public static void marshalPrimitiveList(List list,
DataOutputStream out) throws IOException {
+ out.writeInt(list.size());
+ for (Iterator iter = list.iterator(); iter.hasNext();) {
+ Object element = (Object) iter.next();
+ marshalPrimitive(out, element);
+ }
+ }
+
+ public static List unmarshalPrimitiveList(DataInputStream in)
throws IOException {
+ int size = in.readInt();
+ List answer = new ArrayList(size);
+ while (size-- > 0) {
+ answer.add(unmarshalPrimitive(in));
+ }
+ return answer;
+ }
+
+ static public void marshalPrimitive(DataOutputStream out,
Object value) throws IOException {
+ if( value == null ) {
+ out.writeByte(NULL);
+ } else if( value.getClass() == Boolean.class ) {
+ out.writeByte(BOOLEAN_TYPE);
+ out.writeBoolean(((Boolean)value).booleanValue());
+ } else if( value.getClass() == Byte.class ) {
+ out.writeByte(BYTE_TYPE);
+ out.writeByte(((Byte)value).byteValue());
+ } else if( value.getClass() == Character.class ) {
+ out.writeByte(CHAR_TYPE);
+ out.writeChar(((Character)value).charValue());
+ } else if( value.getClass() == Short.class ) {
+ out.writeByte(SHORT_TYPE);
+ out.writeShort(((Short)value).shortValue());
+ } else if( value.getClass() == Integer.class ) {
+ out.writeByte(INTEGER_TYPE);
+ out.writeInt(((Integer)value).intValue());
+ } else if( value.getClass() == Long.class ) {
+ out.writeByte(LONG_TYPE);
+ out.writeLong(((Long)value).longValue());
+ } else if( value.getClass() == Float.class ) {
+ out.writeByte(FLOAT_TYPE);
+ out.writeFloat(((Float)value).floatValue());
+ } else if( value.getClass() == Double.class ) {
+ out.writeByte(DOUBLE_TYPE);
+ out.writeDouble(((Double)value).doubleValue());
+ } else if( value.getClass() == byte[].class ) {
+ out.writeByte(BYTE_ARRAY_TYPE);
+ out.writeInt(((byte[])value).length);
+ out.write(((byte[])value));
+ } else if( value.getClass() == String.class ) {
+ String s = (String)value;
+
+ // If it's too big, out.writeUTF may not able able to
write it out.
+ if( s.length() < Short.MAX_VALUE/4 ) {
+ out.writeByte(STRING_TYPE);
+ out.writeUTF((String)value);
+ } else {
+ out.writeByte(BIG_STRING_TYPE);
+ writeUTF8(out, s);
+ }
+
+ } else if( value instanceof Map) {
+ out.writeByte(MAP_TYPE);
+ marshalPrimitiveMap((Map) value, out);
+ } else if( value instanceof List) {
+ out.writeByte(LIST_TYPE);
+ marshalPrimitiveList((List) value, out);
+ } else {
+ throw new IOException("Object is not a primitive:
"+value);
+ }
+ }
+
+
+ static public Object unmarshalPrimitive(DataInputStream in)
throws IOException {
+ Object value=null;
+ switch( in.readByte() ) {
+ case BYTE_TYPE:
+ value = new Byte(in.readByte());
+ break;
+ case BOOLEAN_TYPE:
+ value = in.readBoolean() ? Boolean.TRUE : Boolean.FALSE;
+ break;
+ case CHAR_TYPE:
+ value = new Character(in.readChar());
+ break;
+ case SHORT_TYPE:
+ value = new Short(in.readShort());
+ break;
+ case INTEGER_TYPE:
+ value = new Integer(in.readInt());
+ break;
+ case LONG_TYPE:
+ value = new Long(in.readLong());
+ break;
+ case FLOAT_TYPE:
+ value = new Float(in.readFloat());
+ break;
+ case DOUBLE_TYPE:
+ value = new Double(in.readDouble());
+ break;
+ case BYTE_ARRAY_TYPE:
+ value = new byte[in.readInt()];
+ in.readFully((byte[])value);
+ break;
+ case STRING_TYPE:
+ value = in.readUTF();
+ break;
+ case BIG_STRING_TYPE:
+ value = readUTF8(in);
+ break;
+ case MAP_TYPE:
+ value = unmarshalPrimitiveMap(in);
+ break;
+ case LIST_TYPE:
+ value = unmarshalPrimitiveList(in);
+ break;
+ }
+ return value;
+ }
+
+ static public void writeUTF8(DataOutput dataOut, String text)
throws IOException {
+ if (text != null) {
+ int strlen = text.length();
+ int utflen = 0;
+ char[] charr = new char[strlen];
+ int c, count = 0;
+
+ text.getChars(0, strlen, charr, 0);
+
+ for (int i = 0; i < strlen; i++) {
+ c = charr[i];
+ if ((c >= 0x0001) && (c <= 0x007F)) {
+ utflen++;
+ } else if (c > 0x07FF) {
+ utflen += 3;
+ } else {
+ utflen += 2;
+ }
+ }
+ //TODO diff: Sun code - removed
+ byte[] bytearr = new byte[utflen + 4]; //TODO diff:
Sun code
+ bytearr[count++] = (byte) ((utflen >>> 24) & 0xFF); //
TODO diff: Sun code
+ bytearr[count++] = (byte) ((utflen >>> 16) & 0xFF); //
TODO diff: Sun code
+ bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF);
+ bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF);
+ for (int i = 0; i < strlen; i++) {
+ c = charr[i];
+ if ((c >= 0x0001) && (c <= 0x007F)) {
+ bytearr[count++] = (byte) c;
+ } else if (c > 0x07FF) {
+ bytearr[count++] = (byte) (0xE0 | ((c >> 12) &
0x0F));
+ bytearr[count++] = (byte) (0x80 | ((c >> 6) &
0x3F));
+ bytearr[count++] = (byte) (0x80 | ((c >> 0) &
0x3F));
+ } else {
+ bytearr[count++] = (byte) (0xC0 | ((c >> 6) &
0x1F));
+ bytearr[count++] = (byte) (0x80 | ((c >> 0) &
0x3F));
+ }
+ }
+ dataOut.write(bytearr);
+
+ } else {
+ dataOut.writeInt(-1);
+ }
+ }
+
+ static public String readUTF8(DataInput dataIn) throws
IOException {
+ int utflen = dataIn.readInt(); //TODO diff: Sun code
+ if (utflen > -1) {
+ StringBuffer str = new StringBuffer(utflen);
+ byte bytearr[] = new byte[utflen];
+ int c, char2, char3;
+ int count = 0;
+
+ dataIn.readFully(bytearr, 0, utflen);
+
+ while (count < utflen) {
+ c = bytearr[count] & 0xff;
+ switch (c >> 4) {
+ case 0:
+ case 1:
+ case 2:
+ case 3:
+ case 4:
+ case 5:
+ case 6:
+ case 7:
+ /* 0xxxxxxx */
+ count++;
+ str.append((char) c);
+ break;
+ case 12:
+ case 13:
+ /* 110x xxxx 10xx xxxx */
+ count += 2;
+ if (count > utflen) {
+ throw new UTFDataFormatException();
+ }
+ char2 = bytearr[count - 1];
+ if ((char2 & 0xC0) != 0x80) {
+ throw new UTFDataFormatException();
+ }
+ str.append((char) (((c & 0x1F) << 6) |
(char2 & 0x3F)));
+ break;
+ case 14:
+ /* 1110 xxxx 10xx xxxx 10xx xxxx */
+ count += 3;
+ if (count > utflen) {
+ throw new UTFDataFormatException();
+ }
+ char2 = bytearr[count - 2]; //TODO diff:
Sun code
+ char3 = bytearr[count - 1]; //TODO diff:
Sun code
+ if (((char2 & 0xC0) != 0x80) || ((char3 &
0xC0) != 0x80)) {
+ throw new UTFDataFormatException();
+ }
+ str.append((char) (((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)));
+ break;
+ default :
+ /* 10xx xxxx, 1111 xxxx */
+ throw new UTFDataFormatException();
+ }
+ }
+ // The number of chars produced may be less than utflen
+ return new String(str);
+ } else {
+ return null;
+ }
+ }
+
+
+}
+
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/
apache/geronimo/openwire/util/MarshallingSupport.java
----------------------------------------------------------------------
--------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/
apache/geronimo/openwire/util/MarshallingSupport.java
----------------------------------------------------------------------
--------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/
apache/geronimo/openwire/util/MarshallingSupport.java
----------------------------------------------------------------------
--------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/
geronimo/openwire/util/ServiceStopper.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/openwire/
src/main/java/org/apache/geronimo/openwire/util/ServiceStopper.java?
view=auto&rev=446519
======================================================================
========
--- geronimo/sandbox/gcache/openwire/src/main/java/org/apache/
geronimo/openwire/util/ServiceStopper.java (added)
+++ geronimo/sandbox/gcache/openwire/src/main/java/org/apache/
geronimo/openwire/util/ServiceStopper.java Thu Sep 14 21:57:24 2006
@@ -0,0 +1,97 @@
+package org.apache.geronimo.openwire.util;
+
+import org.apache.geronimo.openwire.Service;
+import org.apache.commons.logging.LogFactory;
+import org.apache.commons.logging.Log;
+
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * 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 ServiceStopper {
+ private Throwable firstException;
+
+ /**
+ * Stops the given service, catching any exceptions that are
thrown.
+ */
+ public void stop(Service service) {
+ try {
+ if( service!=null ) {
+ service.stop();
+ }
+ }
+ catch (Exception e) {
+ onException(service, e);
+ }
+ }
+
+ /**
+ * Performs the given code to stop some service handling the
exceptions
+ * which may be thrown properly
+ */
+ public void run(Callback stopClosure) {
+ try {
+ stopClosure.execute();
+ }
+ catch (Throwable e) {
+ onException(stopClosure, e);
+ }
+ }
+
+ /**
+ * Stops a list of services
+ */
+ public void stopServices(List services) {
+ for (Iterator iter = services.iterator(); iter.hasNext();) {
+ Service service = (Service) iter.next();
+ stop(service);
+ }
+ }
+
+ public void onException(Object owner, Throwable e) {
+ logError(owner, e);
+ if (firstException == null) {
+ firstException = e;
+ }
+ }
+
+ /**
+ * Throws the first exception that was thrown if there was one.
+ */
+ public void throwFirstException() throws Exception {
+ if (firstException != null) {
+ if (firstException instanceof Exception) {
+ Exception e = (Exception) firstException;
+ throw e;
+ }
+ else if (firstException instanceof RuntimeException) {
+ RuntimeException e = (RuntimeException)
firstException;
+ throw e;
+ }
+ else {
+ throw new RuntimeException("Unknown type of
exception: " + firstException, firstException);
+ }
+ }
+ }
+
+ protected void logError(Object service, Throwable e) {
+ Log log = LogFactory.getLog(service.getClass());
+ log.error("Could not stop service: " + service + ".
Reason: " + e, e);
+ }
+
+
+}
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/
apache/geronimo/openwire/util/ServiceStopper.java
----------------------------------------------------------------------
--------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/
apache/geronimo/openwire/util/ServiceStopper.java
----------------------------------------------------------------------
--------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/
apache/geronimo/openwire/util/ServiceStopper.java
----------------------------------------------------------------------
--------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/
geronimo/openwire/util/ServiceSupport.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/openwire/
src/main/java/org/apache/geronimo/openwire/util/ServiceSupport.java?
view=auto&rev=446519
======================================================================
========
--- geronimo/sandbox/gcache/openwire/src/main/java/org/apache/
geronimo/openwire/util/ServiceSupport.java (added)
+++ geronimo/sandbox/gcache/openwire/src/main/java/org/apache/
geronimo/openwire/util/ServiceSupport.java Thu Sep 14 21:57:24 2006
@@ -0,0 +1,88 @@
+package org.apache.geronimo.openwire.util;
+
+import
edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
+import org.apache.geronimo.openwire.Service;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * 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 abstract class ServiceSupport implements Service {
+ private static final Log log = LogFactory.getLog
(ServiceSupport.class);
+
+ private AtomicBoolean started = new AtomicBoolean(false);
+ private AtomicBoolean stopping = new AtomicBoolean(false);
+ private AtomicBoolean stopped = new AtomicBoolean(false);
+
+ public static void dispose(Service service) {
+ try {
+ service.stop();
+ }
+ catch (Exception e) {
+ log.debug("Could not stop service: " + service + ".
Reason: " + e, e);
+ }
+ }
+
+ public void start() throws Exception {
+ if (started.compareAndSet(false, true)) {
+ doStart();
+ }
+ }
+
+ public void stop() throws Exception {
+ if (stopped.compareAndSet(false, true)) {
+ stopping.set(true);
+ ServiceStopper stopper = new ServiceStopper();
+ try {
+ doStop(stopper);
+ }
+ catch (Exception e) {
+ stopper.onException(this, e);
+ }
+ stopped.set(true);
+ started.set(false);
+ stopping.set(false);
+ stopper.throwFirstException();
+ }
+ }
+
+ /**
+ * @return true if this service has been started
+ */
+ public boolean isStarted() {
+ return started.get();
+ }
+
+ /**
+ * @return true if this service is in the process of closing
+ */
+ public boolean isStopping() {
+ return stopping.get();
+ }
+
+
+ /**
+ * @return true if this service is closed
+ */
+ public boolean isStopped() {
+ return stopped.get();
+ }
+
+ protected abstract void doStop(ServiceStopper stopper) throws
Exception;
+
+ protected abstract void doStart() throws Exception;
+}
+
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/
apache/geronimo/openwire/util/ServiceSupport.java
----------------------------------------------------------------------
--------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/
apache/geronimo/openwire/util/ServiceSupport.java
----------------------------------------------------------------------
--------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/
apache/geronimo/openwire/util/ServiceSupport.java
----------------------------------------------------------------------
--------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/
geronimo/openwire/util/URISupport.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/openwire/
src/main/java/org/apache/geronimo/openwire/util/URISupport.java?
view=auto&rev=446519
======================================================================
========
--- geronimo/sandbox/gcache/openwire/src/main/java/org/apache/
geronimo/openwire/util/URISupport.java (added)
+++ geronimo/sandbox/gcache/openwire/src/main/java/org/apache/
geronimo/openwire/util/URISupport.java Thu Sep 14 21:57:24 2006
@@ -0,0 +1,295 @@
+package org.apache.geronimo.openwire.util;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URLDecoder;
+import java.net.URLEncoder;
+import java.util.*;
+import java.io.UnsupportedEncodingException;
+
+/**
+ * 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 URISupport {
+
+ public static class CompositeData {
+ String scheme;
+ String path;
+ URI components[];
+ Map parameters;
+ String fragment;
+ public String host;
+
+ public URI[] getComponents() {
+ return components;
+ }
+ public String getFragment() {
+ return fragment;
+ }
+ public Map getParameters() {
+ return parameters;
+ }
+ public String getScheme() {
+ return scheme;
+ }
+ public String getPath() {
+ return path;
+ }
+ public String getHost() {
+ return host;
+ }
+
+ public URI toURI() throws URISyntaxException {
+ StringBuffer sb = new StringBuffer();
+ if( scheme!=null ) {
+ sb.append(scheme);
+ sb.append(':');
+ }
+
+ if( host!=null && host.length()!=0 ) {
+ sb.append(host);
+ } else {
+ sb.append('(');
+ for (int i = 0; i < components.length; i++) {
+ if( i!=0 )
+ sb.append(',');
+ sb.append(components[i].toString());
+ }
+ sb.append(')');
+ }
+
+ if( path !=null ) {
+ sb.append('/');
+ sb.append(path);
+ }
+ if(!parameters.isEmpty()) {
+ sb.append("?");
+ sb.append(createQueryString(parameters));
+ }
+ if( fragment!=null ) {
+ sb.append("#");
+ sb.append(fragment);
+ }
+ return new URI(sb.toString());
+ }
+ }
+
+ public static Map parseQuery(String uri) throws
URISyntaxException{
+ try{
+ Map rc=new HashMap();
+ if(uri!=null){
+ String[] parameters=uri.split("&");
+ for(int i=0;i<parameters.length;i++){
+ int p=parameters[i].indexOf("=");
+ if(p>=0){
+ String name= URLDecoder.decode(parameters
[i].substring(0,p),"UTF-8");
+ String value=URLDecoder.decode(parameters
[i].substring(p+1),"UTF-8");
+ rc.put(name,value);
+ }else{
+ rc.put(parameters[i],null);
+ }
+ }
+ }
+ return rc;
+ }catch(UnsupportedEncodingException e){
+ throw (URISyntaxException) new URISyntaxException
(e.toString(),"Invalid encoding").initCause(e);
+ }
+ }
+
+ public static Map parseParamters(URI uri) throws
URISyntaxException {
+ return uri.getQuery()==null ? Collections.EMPTY_MAP :
parseQuery(stripPrefix(uri.getQuery(), "?"));
+ }
+
+ /**
+ * Removes any URI query from the given uri
+ */
+ public static URI removeQuery(URI uri) throws
URISyntaxException {
+ return createURIWithQuery(uri, null);
+ }
+
+ /**
+ * Creates a URI with the given query
+ */
+ public static URI createURIWithQuery(URI uri, String query)
throws URISyntaxException {
+ return new URI(uri.getScheme(), uri.getUserInfo(),
uri.getHost(), uri.getPort(), uri.getPath(), query, uri.getFragment
());
+ }
+
+ public static CompositeData parseComposite(URI uri) throws
URISyntaxException {
+
+ CompositeData rc = new CompositeData();
+ rc.scheme = uri.getScheme();
+ String ssp = stripPrefix(uri.getSchemeSpecificPart().trim
(), "//").trim();
+
+ parseComposite(uri, rc, ssp);
+
+ rc.fragment = uri.getFragment();
+ return rc;
+ }
+
+ private static void parseComposite(URI uri, CompositeData rc,
String ssp) throws URISyntaxException {
+ String componentString;
+ String params;
+
+ if(!checkParenthesis(ssp)){
+ throw new URISyntaxException(uri.toString(), "Not a
matching number of '(' and ')' parenthesis");
+ }
+
+ int p;
+ int intialParen = ssp.indexOf("(");
+ if( intialParen==0 ) {
+ rc.host = ssp.substring(0, intialParen);
+ p = rc.host.indexOf("/");
+ if( p >= 0 ) {
+ rc.path = rc.host.substring(p);
+ rc.host = rc.host.substring(0,p);
+ }
+ p = ssp.lastIndexOf(")");
+ componentString = ssp.substring(intialParen+1,p);
+ params = ssp.substring(p+1).trim();
+
+ } else {
+ componentString = ssp;
+ params="";
+ }
+
+ String components[] = splitComponents(componentString);
+ rc.components=new URI[components.length];
+ for (int i = 0; i < components.length; i++) {
+ rc.components[i] = new URI(components[i].trim());
+ }
+
+ p = params.indexOf("?");
+ if( p >= 0 ) {
+ if( p > 0) {
+ rc.path = stripPrefix(params.substring(0, p), "/");
+ }
+ rc.parameters = parseQuery(params.substring(p+1));
+ } else {
+ if( params.length() > 0 )
+ rc.path = stripPrefix(params, "/");
+ rc.parameters = Collections.EMPTY_MAP;
+ }
+ }
+
+ private static String[] splitComponents(String str) {
+ ArrayList l = new ArrayList();
+
+ int last=0;
+ int depth = 0;
+ char chars[] = str.toCharArray();
+ for( int i=0; i < chars.length; i ++ ) {
+ switch( chars[i] ) {
+ case '(':
+ depth++;
+ break;
+ case ')':
+ depth--;
+ break;
+ case ',':
+ if( depth == 0 ) {
+ String s = str.substring(last, i);
+ l.add(s);
+ last=i+1;
+ }
+ }
+ }
+
+ String s = str.substring(last);
+ if( s.length() !=0 )
+ l.add(s);
+
+ String rc[] = new String[l.size()];
+ l.toArray(rc);
+ return rc;
+ }
+
+ public static String stripPrefix(String value, String prefix) {
+ if( value.startsWith(prefix) )
+ return value.substring(prefix.length());
+ return value;
+ }
+
+ public static URI stripScheme(URI uri) throws
URISyntaxException {
+ return new URI(stripPrefix(uri.getSchemeSpecificPart().trim
(), "//"));
+ }
+
+ public static String createQueryString(Map options) throws
URISyntaxException {
+ try {
+ if(options.size()>0) {
+ StringBuffer rc = new StringBuffer();
+ boolean first=true;
+ for (Iterator iter = options.keySet().iterator();
iter.hasNext();) {
+ if( first )
+ first=false;
+ else
+ rc.append("&");
+
+ String key = (String) iter.next();
+ String value = (String)options.get(key);
+ rc.append(URLEncoder.encode(key, "UTF-8"));
+ rc.append("=");
+ rc.append(URLEncoder.encode(value, "UTF-8"));
+ }
+ return rc.toString();
+ } else {
+ return "";
+ }
+ } catch (UnsupportedEncodingException e) {
+ throw (URISyntaxException)new URISyntaxException
(e.toString(), "Invalid encoding").initCause(e);
+ }
+ }
+
+ /**
+ * Creates a URI from the original URI and the remaining
paramaters
+ * @throws URISyntaxException
+ */
+ public static URI createRemainingURI(URI originalURI, Map
params) throws URISyntaxException {
+ String s = createQueryString(params);
+ if( s.length()==0 )
+ s = null;
+ return createURIWithQuery(originalURI, s);
+ }
+
+ static public URI changeScheme(URI bindAddr, String scheme)
throws URISyntaxException {
+ return new URI(scheme, bindAddr.getUserInfo(),
bindAddr.getHost(), bindAddr.getPort(), bindAddr.getPath(),
bindAddr.getQuery(), bindAddr.getFragment());
+ }
+
+ public static boolean checkParenthesis(String str){
+ boolean result=true;
+ if(str!=null){
+ int open=0;
+ int closed=0;
+
+ int i=0;
+ while((i=str.indexOf('(',i)) >=0 ){
+ i++;
+ open++;
+ }
+ i=0;
+ while((i=str.indexOf(')',i)) >=0 ){
+ i++;
+ closed++;
+ }
+ result = open == closed;
+ }
+ return result;
+ }
+
+ public int indexOfParenthesisMatch(String str){
+ int result = -1;
+
+ return result;
+ }
+}
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/
apache/geronimo/openwire/util/URISupport.java
----------------------------------------------------------------------
--------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/
apache/geronimo/openwire/util/URISupport.java
----------------------------------------------------------------------
--------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/
apache/geronimo/openwire/util/URISupport.java
----------------------------------------------------------------------
--------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/
geronimo/openwire/wireformat/ObjectStreamWireFormat.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/openwire/
src/main/java/org/apache/geronimo/openwire/wireformat/
ObjectStreamWireFormat.java?view=auto&rev=446519
======================================================================
========
--- geronimo/sandbox/gcache/openwire/src/main/java/org/apache/
geronimo/openwire/wireformat/ObjectStreamWireFormat.java (added)
+++ geronimo/sandbox/gcache/openwire/src/main/java/org/apache/
geronimo/openwire/wireformat/ObjectStreamWireFormat.java Thu Sep 14
21:57:24 2006
@@ -0,0 +1,75 @@
+/**
+ *
+ * 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.wireformat;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.ObjectOutputStream;
+
+import org.apache.geronimo.openwire.util.ByteSequence;
+import org.apache.geronimo.openwire.util.ByteArrayOutputStream;
+import org.apache.geronimo.openwire.util.ByteArrayInputStream;
+import
org.apache.geronimo.openwire.util.ClassLoadingAwareObjectInputStream;
+
+/**
+ * A simple implementation which uses Object Stream serialization.
+ *
+ * @version $Revision$
+ */
+public class ObjectStreamWireFormat implements WireFormat {
+
+ public ByteSequence marshal(Object command) throws IOException {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ DataOutputStream ds = new DataOutputStream(baos);
+ marshal(command, ds);
+ ds.close();
+ return baos.toByteSequence();
+ }
+
+ public Object unmarshal(ByteSequence packet) throws IOException {
+ return unmarshal(new DataInputStream(new
ByteArrayInputStream(packet)));
+ }
+
+ public void marshal(Object command, DataOutputStream ds)
throws IOException {
+ ObjectOutputStream out = new ObjectOutputStream(ds);
+ out.writeObject(command);
+ out.flush();
+ out.reset();
+ }
+
+ public Object unmarshal(DataInputStream ds) throws IOException {
+ try {
+ ClassLoadingAwareObjectInputStream in = new
ClassLoadingAwareObjectInputStream(ds);
+ Object command;
+ command = in.readObject();
+ in.close();
+ return command;
+ } catch (ClassNotFoundException e) {
+ throw (IOException)new IOException("unmarshal failed:
"+e).initCause(e);
+ }
+ }
+
+ public void setVersion(int version) {
+ }
+
+ public int getVersion() {
+ return 0;
+ }
+
+}
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/
apache/geronimo/openwire/wireformat/ObjectStreamWireFormat.java
----------------------------------------------------------------------
--------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/
apache/geronimo/openwire/wireformat/ObjectStreamWireFormat.java
----------------------------------------------------------------------
--------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/
apache/geronimo/openwire/wireformat/ObjectStreamWireFormat.java
----------------------------------------------------------------------
--------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/
geronimo/openwire/wireformat/WireFormat.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/openwire/
src/main/java/org/apache/geronimo/openwire/wireformat/
WireFormat.java?view=auto&rev=446519
======================================================================
========
--- geronimo/sandbox/gcache/openwire/src/main/java/org/apache/
geronimo/openwire/wireformat/WireFormat.java (added)
+++ geronimo/sandbox/gcache/openwire/src/main/java/org/apache/
geronimo/openwire/wireformat/WireFormat.java Thu Sep 14 21:57:24 2006
@@ -0,0 +1,65 @@
+/**
+ *
+ * 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.wireformat;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+
+import org.apache.geronimo.openwire.util.ByteSequence;
+
+
+/**
+ * Provides a mechanism to marshal commands into and out of packets
+ * or into and out of streams, Channels and Datagrams.
+ *
+ * @version $Revision$
+ */
+public interface WireFormat {
+
+ /**
+ * Packet based marshaling
+ */
+ ByteSequence marshal(Object command) throws IOException;
+
+ /**
+ * Packet based un-marshaling
+ */
+ Object unmarshal(ByteSequence packet) throws IOException;
+
+ /**
+ * Stream based marshaling
+ */
+ void marshal(Object command, DataOutputStream out) throws
IOException;
+
+ /**
+ * Packet based un-marshaling
+ */
+ Object unmarshal(DataInputStream in) throws IOException;
+
+ /**
+ * @param version - the version of the wire format
+ */
+ public void setVersion(int version);
+
+ /**
+ * @return the version of the wire format
+ */
+ public int getVersion();
+
+}
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/
apache/geronimo/openwire/wireformat/WireFormat.java
----------------------------------------------------------------------
--------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/
apache/geronimo/openwire/wireformat/WireFormat.java
----------------------------------------------------------------------
--------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/
apache/geronimo/openwire/wireformat/WireFormat.java
----------------------------------------------------------------------
--------
svn:mime-type = text/plain
Added: geronimo/sandbox/gcache/openwire/src/main/java/org/apache/
geronimo/openwire/wireformat/WireFormatFactory.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/openwire/
src/main/java/org/apache/geronimo/openwire/wireformat/
WireFormatFactory.java?view=auto&rev=446519
======================================================================
========
--- geronimo/sandbox/gcache/openwire/src/main/java/org/apache/
geronimo/openwire/wireformat/WireFormatFactory.java (added)
+++ geronimo/sandbox/gcache/openwire/src/main/java/org/apache/
geronimo/openwire/wireformat/WireFormatFactory.java Thu Sep 14
21:57:24 2006
@@ -0,0 +1,22 @@
+/**
+ *
+ * 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.wireformat;
+
+public interface WireFormatFactory {
+ WireFormat createWireFormat();
+}
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/
apache/geronimo/openwire/wireformat/WireFormatFactory.java
----------------------------------------------------------------------
--------
svn:eol-style = native
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/
apache/geronimo/openwire/wireformat/WireFormatFactory.java
----------------------------------------------------------------------
--------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/gcache/openwire/src/main/java/org/
apache/geronimo/openwire/wireformat/WireFormatFactory.java
----------------------------------------------------------------------
--------
svn:mime-type = text/plain
Modified: geronimo/sandbox/gcache/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gcache/pom.xml?
view=diff&rev=446519&r1=446518&r2=446519
======================================================================
========
--- geronimo/sandbox/gcache/pom.xml (original)
+++ geronimo/sandbox/gcache/pom.xml Thu Sep 14 21:57:24 2006
@@ -17,6 +17,7 @@
</scm>
<modules>
+ <module>openwire</module>
<module>client</module>
<module>server</module>
</modules>
@@ -42,6 +43,12 @@
<groupId>backport-util-concurrent</groupId>
<artifactId>backport-util-concurrent</artifactId>
<version>2.0_01_pd</version>
+ </dependency>
+
+ <dependency>
+ <groupId>commons-logging</groupId>
+ <artifactId>commons-logging</artifactId>
+ <version>1.0.4</version>
</dependency>
<dependency>