axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From antel...@apache.org
Subject cvs commit: xml-axis-wsif/java/src/org/apache/wsif/base WSIFDefaultCorrelationService.java
Date Thu, 01 Aug 2002 10:03:21 GMT
antelder    2002/08/01 03:03:21

  Modified:    java/test/async CorrelationServiceTest.java
               java/src/org/apache/wsif/base
                        WSIFDefaultCorrelationService.java
  Log:
  Fix the correlation service to work with primative classes
  
  Revision  Changes    Path
  1.2       +21 -0     xml-axis-wsif/java/test/async/CorrelationServiceTest.java
  
  Index: CorrelationServiceTest.java
  ===================================================================
  RCS file: /home/cvs/xml-axis-wsif/java/test/async/CorrelationServiceTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- CorrelationServiceTest.java	6 Jun 2002 08:28:54 -0000	1.1
  +++ CorrelationServiceTest.java	1 Aug 2002 10:03:20 -0000	1.2
  @@ -148,6 +148,27 @@
               s = (String) cs.get(cid);
               assertTrue(s == null); // due to remove
   
  +			// test primative clasess
  +			Class[] clss =
  +				new Class[] {
  +					int.class,
  +					float.class,
  +					long.class,
  +					double.class,
  +					short.class,
  +					byte.class,
  +					boolean.class,
  +					void.class };
  +			Class[] clss2;
  +			cid = new WSIFJMSCorrelationId("P1");
  +			cs.put(cid, clss, (long) 0);
  +			clss2 = (Class[]) cs.get(cid);
  +			for (int i = 0; i < clss.length; i++) {
  +				assertTrue(
  +					"class " + clss[i] + " failed!!",
  +					clss[i] == clss2[i]);
  +			}
  +			
               System.out.println("WSIFCorrealtionService tests complete.");
           } catch (WSIFException ex) {
               ex.printStackTrace();
  
  
  
  1.5       +239 -187  xml-axis-wsif/java/src/org/apache/wsif/base/WSIFDefaultCorrelationService.java
  
  Index: WSIFDefaultCorrelationService.java
  ===================================================================
  RCS file: /home/cvs/xml-axis-wsif/java/src/org/apache/wsif/base/WSIFDefaultCorrelationService.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- WSIFDefaultCorrelationService.java	19 Jul 2002 15:46:29 -0000	1.4
  +++ WSIFDefaultCorrelationService.java	1 Aug 2002 10:03:20 -0000	1.5
  @@ -60,9 +60,12 @@
   import java.io.ByteArrayInputStream;
   import java.io.ByteArrayOutputStream;
   import java.io.IOException;
  +import java.io.InputStream;
   import java.io.ObjectInputStream;
   import java.io.ObjectOutputStream;
  +import java.io.ObjectStreamClass;
   import java.io.Serializable;
  +import java.io.StreamCorruptedException;
   import java.util.ArrayList;
   import java.util.ConcurrentModificationException;
   import java.util.HashMap;
  @@ -82,191 +85,240 @@
    */
   public class WSIFDefaultCorrelationService implements WSIFCorrelationService {
   
  -    private HashMap correlatorStore; // associates IDs with WSIFOperators  
  -    private HashMap timeouts; // associates IDs with a timeout time
  -    private Thread timeoutWatcher; // watches for timeout times expiring	
  -    private boolean shutdown; // has the correlation service been shutdown  
  -
  -    /**
  -     * WSIFCorrelationServiceLocator should be used to
  -     * create a correlation service.
  -     */
  -    public WSIFDefaultCorrelationService() {
  -        Trc.entry(this);
  -        Trc.exit();
  -    }
  -
  -    /**
  -     * Adds an entry to the correlation service.
  -     * @param correlator   the key to associate with the state. This will be 
  -     *                     a JMS message correlation ID.
  -     * @param state   the state to be stored. This will be a WSIFOperation.
  -     * @param timeout   a timeout period after which the key and associated
  -     *                  state will be deleted from the correlation service. A
  -     *                  value of zero indicates there should be no timeout. 
  -     */
  -    public synchronized void put(
  -        WSIFCorrelationId correlator,
  -        Serializable state,
  -        long timeout)
  -        throws WSIFException {
  -        Trc.entry(this, correlator, state, new Long(timeout));
  -        if (correlator != null && state != null) {
  -            if (correlatorStore == null) {
  -                initialise();
  -            }
  -            try {
  -                correlatorStore.put(correlator, serialize(state));
  -		        if ( timeout > 0 ) {
  -		       	   if ( timeouts == null ) {
  -		       	      initTimeouts();
  -		       	   }
  -  		           timeouts.put( correlator, 
  -  		                         new Long( System.currentTimeMillis()+timeout ) );
  -                }
  -            } catch (IOException ex) {
  -                throw new WSIFException(ex.toString());
  -            }
  -        } else {
  -            throw new IllegalArgumentException(
  -                "cannot put null " + ((correlator == null) ? "correlator" : "state"));
  -        }
  -        Trc.exit();
  -    }
  -
  -    /**
  -     * Retrieves an entry (a WSIFOperation) from the correlation service.
  -     * @param id   the key of the state to retrieved
  -     * @return the state associated with the id, or null if there is no
  -     *         match for the id. 
  -     */
  -    public synchronized Serializable get(WSIFCorrelationId id)
  -        throws WSIFException {
  -        Trc.entry(this, id);
  -        if (correlatorStore == null) {
  -            throw new WSIFException("get called on correlation service but put never done");
  -        } else if (id == null) {
  -            throw new IllegalArgumentException("cannot get null");
  -        } else {
  -            try {
  -                Serializable s = (Serializable) unserialize((byte[]) correlatorStore.get(id));
  -                Trc.exit(s);
  -                return s;
  -            } catch (Exception ex) {
  -                throw new WSIFException(ex.toString());
  -            }
  -        }
  -    }
  -
  -    /**
  -     * Removes an entry form the correlation service.
  -     * @param id   the key of entry to be removed
  -     */
  -    public synchronized void remove(WSIFCorrelationId id) throws WSIFException {
  -        Trc.entry(this, id);
  -        if (correlatorStore == null) {
  -            throw new WSIFException("corelation service has been shutdown");
  -        } else if (id == null) {
  -            throw new IllegalArgumentException("cannot remove null");
  -        } else {
  -            correlatorStore.remove(id);
  -            if ( timeouts != null ) {
  -              timeouts.remove(id);
  -            }
  -            Trc.exit();
  -        }
  -    }
  -
  -    private synchronized void remove(ArrayList expiredKeys) {
  -        Trc.entry(this, expiredKeys);
  -        if (expiredKeys != null && correlatorStore != null) {
  -            Serializable id;
  -            for (Iterator i = expiredKeys.iterator(); i.hasNext();) {
  -                id = (Serializable) i.next();
  -                correlatorStore.remove(id);
  -                timeouts.remove(id);
  -                MessageLogger.log("WSIF.0008W",id);
  -            }
  -        }
  -        Trc.exit();
  -    }
  -
  -    /**
  -     * Shutsdown the correlation service.
  -     */
  -    public void shutdown() {
  -        Trc.entry(this);
  -        shutdown = true;
  -        Trc.exit();
  -    }
  -
  -    private void initialise() {
  -       shutdown = false;
  -       correlatorStore = new HashMap();
  -    }    	
  -
  -    private void initTimeouts() {
  -       timeouts = new HashMap();
  -       timeoutWatcher = new Thread() {
  -          public void run() {
  -             while ( !shutdown ) {
  -                try {
  -             	   sleep(WSIFConstants.CORRELATION_TIMEOUT_DELAY);
  -                } catch (InterruptedException ex) {}
  -                checkForTimeouts();
  -             }
  -             if ( correlatorStore != null ) correlatorStore = null;
  -             if ( timeouts != null ) timeouts = null;
  -		  }
  -	   };
  -       timeoutWatcher.setName( "WSIFDefaultCorrelationService timeout watcher" );
  -       timeoutWatcher.start();
  -    }    	
  -
  -    private void checkForTimeouts() {
  -        Long expireTime;
  -        Serializable key;
  -        ArrayList expiredKeys = new ArrayList();
  -        Long now = new Long(System.currentTimeMillis());
  -        // add to expiredKeys all the keys whose timouts have expired 
  -        try {
  -            for (Iterator i = timeouts.keySet().iterator(); i.hasNext();) {
  -                key = (Serializable) i.next();
  -                expireTime = (Long) timeouts.get(key);
  -                if (now.compareTo(expireTime) > 0) { // now greater than expireTime
  -                    expiredKeys.add(key);
  -                }
  -            }
  -        } catch (ConcurrentModificationException ex) {
  -        } // ignore this, get the others next time
  -
  -        if (expiredKeys.size() > 0) {
  -            remove(expiredKeys);
  -        }
  -
  -    }
  -
  -    private byte[] serialize(Object o) throws IOException {
  -        if (o == null) {
  -            return null;
  -        } else {
  -            ByteArrayOutputStream baos = new ByteArrayOutputStream();
  -            ObjectOutputStream so = new ObjectOutputStream(baos);
  -            so.writeObject(o);
  -            so.flush();
  -            return baos.toByteArray();
  -        }
  -    }
  -
  -    private Object unserialize(byte[] bytes)
  -        throws IOException, ClassNotFoundException {
  -        if (bytes == null) {
  -            return null;
  -        } else {
  -            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
  -            ObjectInputStream si = new ObjectInputStream(bais);
  -            return si.readObject();
  -        }
  -    }
  +	private HashMap correlatorStore; // associates IDs with WSIFOperators  
  +	private HashMap timeouts; // associates IDs with a timeout time
  +	private Thread timeoutWatcher; // watches for timeout times expiring	
  +	private boolean shutdown; // has the correlation service been shutdown  
  +
  +	/**
  +	 * WSIFCorrelationServiceLocator should be used to
  +	 * create a correlation service.
  +	 */
  +	public WSIFDefaultCorrelationService() {
  +		Trc.entry(this);
  +		Trc.exit();
  +	}
  +
  +	/**
  +	 * Adds an entry to the correlation service.
  +	 * @param correlator   the key to associate with the state. This will be 
  +	 *                     a JMS message correlation ID.
  +	 * @param state   the state to be stored. This will be a WSIFOperation.
  +	 * @param timeout   a timeout period after which the key and associated
  +	 *                  state will be deleted from the correlation service. A
  +	 *                  value of zero indicates there should be no timeout. 
  +	 */
  +	public synchronized void put(
  +		WSIFCorrelationId correlator,
  +		Serializable state,
  +		long timeout)
  +		throws WSIFException {
  +		Trc.entry(this, correlator, state, new Long(timeout));
  +		if (correlator != null && state != null) {
  +			if (correlatorStore == null) {
  +				initialise();
  +			}
  +			try {
  +				correlatorStore.put(correlator, serialize(state));
  +				if (timeout > 0) {
  +					if (timeouts == null) {
  +						initTimeouts();
  +					}
  +					timeouts.put(
  +						correlator,
  +						new Long(System.currentTimeMillis() + timeout));
  +				}
  +			} catch (IOException ex) {
  +				throw new WSIFException(ex.toString());
  +			}
  +		} else {
  +			throw new IllegalArgumentException(
  +				"cannot put null "
  +					+ ((correlator == null) ? "correlator" : "state"));
  +		}
  +		Trc.exit();
  +	}
  +
  +	/**
  +	 * Retrieves an entry (a WSIFOperation) from the correlation service.
  +	 * @param id   the key of the state to retrieved
  +	 * @return the state associated with the id, or null if there is no
  +	 *         match for the id. 
  +	 */
  +	public synchronized Serializable get(WSIFCorrelationId id)
  +		throws WSIFException {
  +		Trc.entry(this, id);
  +		if (correlatorStore == null) {
  +			throw new WSIFException("get called on correlation service but put never done");
  +		} else if (id == null) {
  +			throw new IllegalArgumentException("cannot get null");
  +		} else {
  +			try {
  +				Serializable s =
  +					(Serializable) unserialize((byte[]) correlatorStore
  +						.get(id));
  +				Trc.exit(s);
  +				return s;
  +			} catch (Exception ex) {
  +				throw new WSIFException(ex.toString());
  +			}
  +		}
  +	}
  +
  +	/**
  +	 * Removes an entry form the correlation service.
  +	 * @param id   the key of entry to be removed
  +	 */
  +	public synchronized void remove(WSIFCorrelationId id)
  +		throws WSIFException {
  +		Trc.entry(this, id);
  +		if (correlatorStore == null) {
  +			throw new WSIFException("corelation service has been shutdown");
  +		} else if (id == null) {
  +			throw new IllegalArgumentException("cannot remove null");
  +		} else {
  +			correlatorStore.remove(id);
  +			if (timeouts != null) {
  +				timeouts.remove(id);
  +			}
  +			Trc.exit();
  +		}
  +	}
  +
  +	private synchronized void remove(ArrayList expiredKeys) {
  +		Trc.entry(this, expiredKeys);
  +		if (expiredKeys != null && correlatorStore != null) {
  +			Serializable id;
  +			for (Iterator i = expiredKeys.iterator(); i.hasNext();) {
  +				id = (Serializable) i.next();
  +				correlatorStore.remove(id);
  +				timeouts.remove(id);
  +				MessageLogger.log("WSIF.0008W", id);
  +			}
  +		}
  +		Trc.exit();
  +	}
  +
  +	/**
  +	 * Shutsdown the correlation service.
  +	 */
  +	public void shutdown() {
  +		Trc.entry(this);
  +		shutdown = true;
  +		Trc.exit();
  +	}
  +
  +	private void initialise() {
  +		shutdown = false;
  +		correlatorStore = new HashMap();
  +	}
  +
  +	private void initTimeouts() {
  +		timeouts = new HashMap();
  +		timeoutWatcher = new Thread() {
  +			public void run() {
  +				while (!shutdown) {
  +					try {
  +						sleep(WSIFConstants.CORRELATION_TIMEOUT_DELAY);
  +					} catch (InterruptedException ex) {
  +					}
  +					checkForTimeouts();
  +				}
  +				if (correlatorStore != null)
  +					correlatorStore = null;
  +				if (timeouts != null)
  +					timeouts = null;
  +			}
  +		};
  +		timeoutWatcher.setName("WSIFDefaultCorrelationService timeout watcher");
  +		timeoutWatcher.start();
  +	}
  +
  +	private void checkForTimeouts() {
  +		Long expireTime;
  +		Serializable key;
  +		ArrayList expiredKeys = new ArrayList();
  +		Long now = new Long(System.currentTimeMillis());
  +		// add to expiredKeys all the keys whose timouts have expired 
  +		try {
  +			for (Iterator i = timeouts.keySet().iterator(); i.hasNext();) {
  +				key = (Serializable) i.next();
  +				expireTime = (Long) timeouts.get(key);
  +				if (now.compareTo(expireTime) > 0) {
  +					// now greater than expireTime
  +					expiredKeys.add(key);
  +				}
  +			}
  +		} catch (ConcurrentModificationException ex) {
  +		} // ignore this, get the others next time
  +
  +		if (expiredKeys.size() > 0) {
  +			remove(expiredKeys);
  +		}
  +
  +	}
  +
  +	private byte[] serialize(Object o) throws IOException {
  +		if (o == null) {
  +			return null;
  +		} else {
  +			ByteArrayOutputStream baos = new ByteArrayOutputStream();
  +			ObjectOutputStream so = new ObjectOutputStream(baos);
  +			so.writeObject(o);
  +			so.flush();
  +			return baos.toByteArray();
  +		}
  +	}
  +
  +	private Object unserialize(byte[] bytes)
  +		throws IOException, ClassNotFoundException {
  +		if (bytes == null) {
  +			return null;
  +		} else {
  +			ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
  +			//ObjectInputStream si = new ObjectInputStream(bais);
  +			FixedObjectInputStream si = new FixedObjectInputStream(bais);
  +			return si.readObject();
  +		}
  +	}
   
  -}
  \ No newline at end of file
  +}
  +
  +/**
  + * ObjectInputStream is broken, see Java Bug Id 4171142.
  + */
  +class FixedObjectInputStream extends ObjectInputStream {
  +	
  +	public FixedObjectInputStream(InputStream is)
  +		throws IOException, StreamCorruptedException {
  +		super(is);
  +	}
  +
  +	public Class resolveClass(ObjectStreamClass desc)
  +		throws ClassNotFoundException, IOException {
  +		try {
  +			return super.resolveClass( desc );
  +		} catch (ClassNotFoundException ex) {
  +			if ( desc.getName().equals("int") ) {
  +				return int.class;
  +			} else if ( desc.getName().equals("float") ) {
  +				return float.class;
  +			} else if ( desc.getName().equals("double") ) {
  +				return double.class;
  +			} else if ( desc.getName().equals("boolean") ) {
  +				return boolean.class;
  +			} else if ( desc.getName().equals("long") ) {
  +				return long.class;
  +			} else if ( desc.getName().equals("short") ) {
  +				return short.class;
  +			} else if ( desc.getName().equals("byte") ) {
  +				return byte.class;
  +			} else if ( desc.getName().equals("void") ) {
  +				return void.class;
  +			} else {
  +				throw ex;
  +			}
  +		}
  +	}
  +
  +}
  
  
  

Mime
View raw message