Added: river/tck/src/com/sun/jini/compat/start/ServiceDestroyer.java
URL:
http://svn.apache.org/viewvc/river/tck/src/com/sun/jini/compat/start/ServiceDestroyer.java?rev=1234278&view=auto
==============================================================================
--- river/tck/src/com/sun/jini/compat/start/ServiceDestroyer.java (added)
+++ river/tck/src/com/sun/jini/compat/start/ServiceDestroyer.java Sat Jan 21
07:28:27 2012
@@ -0,0 +1,411 @@
+/*
+ *
+ * Copyright 2005 Sun Microsystems, Inc.
+ *
+ * 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
+ *
+ * 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 com.sun.jini.compat.start;
+
+import com.sun.jini.admin.DestroyAdmin;
+import com.sun.jini.admin.StorageLocationAdmin;
+
+import net.jini.admin.Administrable;
+import net.jini.admin.JoinAdmin;
+
+import java.io.File;
+
+import java.rmi.activation.ActivationException;
+import java.rmi.activation.UnknownObjectException;
+import java.rmi.activation.UnknownGroupException;
+import java.rmi.activation.ActivationGroup;
+import java.rmi.activation.ActivationGroupID;
+import java.rmi.activation.ActivationID;
+
+import java.rmi.RemoteException;
+
+/**
+ * This class provides static methods that can be used to destroy a service.
+ *
+ * @author Sun Microsystems, Inc.
+ *
+ */
+public class ServiceDestroyer {
+
+ public static final int DESTROY_SUCCESS = 0;
+ /* Failure return codes */
+ public static final int SERVICE_NOT_ADMINISTRABLE = -1;
+ public static final int SERVICE_NOT_DESTROY_ADMIN = -2;
+ public static final int DEACTIVATION_TIMEOUT = -3;
+ public static final int PERSISTENT_STORE_EXISTS = -4;
+
+ public static final int N_MS_PER_SEC = 1000;
+ public static final int DEFAULT_N_SECS_WAIT = 600;
+
+ /**
+ * Administratively destroys the service referenced by the input
+ * parameter. The service input to this method must implement
+ * both <code>net.jini.admin.Administrable</code> and the
+ * <code>com.sun.jini.admin.DestroyAdmin</code> interfaces
+ * in order for this method to successfully destroy the service.
+ *
+ * @param service reference to the service to destroy
+ *
+ * @return <code>true</code> if the service's destroy method was invoked
+ * successfully; <code>false</code> otherwise.
+ *
+ * @throws java.rmi.RemoteException typically, this exception occurs when
+ * there is a communication failure between the client and the
+ * service's backend. When this exception does occur, the
+ * service may or may not have been successfully destroyed.
+ *
+ * @see net.jini.admin.Administrable
+ * @see net.jini.admin.Administrable#getAdmin
+ * @see com.sun.jini.admin.DestroyAdmin
+ * @see com.sun.jini.admin.DestroyAdmin#destroy
+ */
+ public static int destroy(Object service) throws RemoteException {
+ /* First, test that the service implements both of the appropriate
+ * administration interfaces
+ */
+ DestroyAdmin destroyAdmin = null;
+ if( !(service instanceof Administrable) ) {
+ return SERVICE_NOT_ADMINISTRABLE;
+ }
+ Object admin = ((Administrable)service).getAdmin();
+ if( !(admin instanceof DestroyAdmin) ) {
+ return SERVICE_NOT_DESTROY_ADMIN;
+ }
+ destroyAdmin = (DestroyAdmin)admin;
+ /* Destroy the service */
+ destroyAdmin.destroy();
+ return DESTROY_SUCCESS;
+ }//end destroy
+
+ /**
+ * Administratively destroys the service referenced in the
+ * <code>proxy</code> field of the <code>created</code> parameter, using
+ * a default number of seconds to wait for the service's activation
+ * group to be unregistered with the activation system.
+ *
+ * @param created <code>ServiceStarter.created</code> data structure
+ * containing a reference to the service to destroy and
+ * the id of that service's activation group
+ * @param nSecsWait the number of seconds to wait for the service's
+ * activation group to be no longer registered with
+ * the activation system
+ *
+ * @return <code>int</code> value that indicates either success or
+ * one of a number of possible reasons for failure to destroy
+ * the service.
+ *
+ * @throws java.rmi.RemoteException typically, this exception occurs when
+ * there is a communication failure between the client and the
+ * service's backend. When this exception does occur, the
+ * service may or may not have been successfully destroyed.
+ * @throws java.rmi.activation.ActivationException typically, this
+ * exception occurs when problems arise while attempting to
+ * interact with the activation system
+ */
+ public static int destroy(ServiceStarter.Created created)
+ throws RemoteException,
+ ActivationException
+ {
+ return destroy(created.gid,created.proxy,DEFAULT_N_SECS_WAIT);
+ }//end destroy
+
+ /**
+ * Administratively destroys the service referenced in the
+ * <code>proxy</code> field of the <code>created</code> parameter.
+ *
+ * @param created <code>ServiceStarter.created</code> data structure
+ * containing a reference to the service to destroy and
+ * the id of that service's activation group
+ * @param nSecsWait the number of seconds to wait for the service's
+ * activation group to be no longer registered with
+ * the activation system
+ *
+ * @return <code>int</code> value that indicates either success or
+ * one of a number of possible reasons for failure to destroy
+ * the service.
+ *
+ * @throws java.rmi.RemoteException typically, this exception occurs when
+ * there is a communication failure between the client and the
+ * service's backend. When this exception does occur, the
+ * service may or may not have been successfully destroyed.
+ * @throws java.rmi.activation.ActivationException typically, this
+ * exception occurs when problems arise while attempting to
+ * interact with the activation system
+ */
+ public static int destroy(ServiceStarter.Created created, int nSecsWait)
+ throws RemoteException,
+ ActivationException
+ {
+ return destroy(created.gid,created.proxy,nSecsWait);
+ }//end destroy
+
+ /**
+ * Administratively destroys the service referenced by the
+ * <code>proxy</code> parameter, using a default number of seconds
+ * to wait for the service's activation group to be unregistered
+ * with the activation system.
+ *
+ * @param gid the id of the activation group in which the service to
+ * destroy is registered
+ * @param proxy the reference to the service to destroy
+ *
+ * @return <code>int</code> value that indicates either success or
+ * one of a number of possible reasons for failure to destroy
+ * the service.
+ *
+ * @throws java.rmi.RemoteException typically, this exception occurs when
+ * there is a communication failure between the client and the
+ * service's backend. When this exception does occur, the
+ * service may or may not have been successfully destroyed.
+ * @throws java.rmi.activation.ActivationException typically, this
+ * exception occurs when problems arise while attempting to
+ * interact with the activation system
+ */
+ public static int destroy(ActivationGroupID gid,
+ Object proxy) throws RemoteException,
+ ActivationException
+ {
+ return destroy(gid,proxy,DEFAULT_N_SECS_WAIT);
+ }//end destroy
+
+
+ /**
+ * Administratively destroys the service referenced by the
+ * <code>proxy</code> parameter. This method attempts to verify
+ * that the desired service is indeed destroyed by verifying that
+ * the service's activation group is no longer registered with the
+ * activation system, and by verifying that the directory in which
+ * the service stored its persistent state was deleted.
+ *
+ * @param gid the id of the activation group in which the service to
+ * destroy is registered
+ * @param proxy the reference to the service to destroy
+ * @param nSecsWait the number of seconds to wait for the service's
+ * activation group to be no longer registered with
+ * the activation system
+ *
+ * @return <code>int</code> value that indicates either success or
+ * one of a number of possible reasons for failure to destroy
+ * the service. Possible values are:
+ * <p><ul>
+ * <li> ServiceDestroyer.DESTROY_SUCCESS
+ * <li> ServiceDestroyer.SERVICE_NOT_ADMINISTRABLE - returned when
+ * the service to destroy is not an instance of
+ * net.jini.admin.Administrable
+ * <li> ServiceDestroyer.SERVICE_NOT_DESTROY_ADMIN - returned when
+ * the service to destroy is not an instance of
+ * com.sun.jini.admin.DestroyAdmin
+ * <li> ServiceDestroyer.DEACTIVATION_TIMEOUT - returned when the
+ * service's activation group is still registered with the
+ * activation system after the number of seconds to wait have passed
+ * <li> ServiceDestroyer.PERSISTENT_STORE_EXISTS - returned when the
+ * directory in which the service stores its persistent state
+ * still exists after the service has been successfully destroyed
+ * </ul>
+ *
+ * @throws java.rmi.RemoteException typically, this exception occurs when
+ * there is a communication failure between the client and the
+ * service's backend. When this exception does occur, the
+ * service may or may not have been successfully destroyed.
+ * @throws java.rmi.activation.ActivationException typically, this
+ * exception occurs when problems arise while attempting to
+ * interact with the activation system
+ */
+ public static int destroy(ActivationGroupID gid,
+ Object proxy,
+ int nSecsWait) throws RemoteException,
+ ActivationException
+ {
+ /* Retrieve the current location of the service's persistent store. */
+ String logDir = null;
+ if(proxy instanceof Administrable) {
+ Object admin = ((Administrable)proxy).getAdmin();
+ if(admin instanceof StorageLocationAdmin) {
+ StorageLocationAdmin storeAdmin = (StorageLocationAdmin)admin;
+ logDir = storeAdmin.getStorageLocation();
+ }//endif
+ }//endif
+ /* Destroy the service */
+ int destroyCode = destroy(proxy);
+ if(destroyCode != DESTROY_SUCCESS) return destroyCode;
+ /* Verify the service has actually been destroyed by waiting until
+ * service's activation group is no longer registered with the
+ * activation system.
+ *
+ * Since an exception will be thrown when an attempt is made to
+ * retrieve an activation descriptor for a group which is not
+ * registered, this method makes repeated attempts to retrieve the
+ * activation group descriptor until such an exception is thrown,
+ * or until the indicated number of seconds to wait has passed.
+ */
+ boolean deactivated = false;
+ for(int i=0;i<nSecsWait;i++) {
+ try {
+ ActivationGroup.getSystem().getActivationGroupDesc(gid);
+ } catch (UnknownGroupException e) {
+ deactivated = true;
+ break;
+ }//end try
+ try {
+ Thread.sleep(1*N_MS_PER_SEC);
+ } catch (InterruptedException e) { }
+ }//end loop
+ if(!deactivated) return DEACTIVATION_TIMEOUT;
+ /* Determine if the service's persistent store was successfully
+ * destroyed.
+ */
+ if(logDir != null) {
+ File logDirFD = new File(logDir);
+ if( logDirFD.exists() && logDirFD.isDirectory() ) {
+ return PERSISTENT_STORE_EXISTS;
+ }
+ }
+ return DESTROY_SUCCESS;
+ }//end destroy
+
+
+ /**
+ * Administratively destroys the service referenced in the
+ * <code>proxy</code> field of the <code>created</code> parameter,
+ * which is assumed to be running under a shared VM environment.
+ *
+ * @param created <code>ServiceStarter.Created</code> data structure
+ * containing a reference to the service to destroy, that
+ * service's activation descriptor, and the id of that
+ * service's activation group
+ * @param nSecsWait the number of seconds to wait for the service's
+ * activation descriptor to be no longer registered with
+ * the activation system
+ *
+ * @return <code>int</code> value that indicates either success or
+ * one of a number of possible reasons for failure to destroy
+ * the service.
+ *
+ * @throws java.rmi.RemoteException typically, this exception occurs when
+ * there is a communication failure between the client and the
+ * service's backend. When this exception does occur, the
+ * service may or may not have been successfully destroyed.
+ * @throws java.rmi.activation.ActivationException typically, this
+ * exception occurs when problems arise while attempting to
+ * interact with the activation system
+ */
+ public static int destroyShared(ServiceStarter.Created created,
+ int nSecsWait)
+ throws RemoteException,
+ ActivationException
+ {
+ return destroyShared(created.gid,created.aid,created.proxy,nSecsWait);
+ }//end destroyShared
+
+ /**
+ * Administratively destroys the service referenced by the
+ * <code>proxy</code> parameter, which is assumed to be running
+ * under a shared VM environment. This method attempts to verify
+ * that the desired service is indeed destroyed by verifying that
+ * the service's activation information/descriptor is no longer
+ * registered with the activation system.
+ *
+ * @param gid the id of the activation group in which the service to
+ * destroy is registered
+ * @param id the activation ID of the service to destroy
+ * @param proxy the reference to the service to destroy
+ * @param nSecsWait the number of seconds to wait for the service's
+ * activation descriptor to be no longer registered with
+ * the activation system
+ *
+ * @return <code>int</code> value that indicates either success or
+ * one of a number of possible reasons for failure to destroy
+ * the service. Possible values are:
+ * <p><ul>
+ * <li> ServiceDestroyer.DESTROY_SUCCESS
+ * <li> ServiceDestroyer.SERVICE_NOT_ADMINISTRABLE - returned when
+ * the service to destroy is not an instance of
+ * net.jini.admin.Administrable
+ * <li> ServiceDestroyer.SERVICE_NOT_DESTROY_ADMIN - returned when
+ * the service to destroy is not an instance of
+ * com.sun.jini.admin.DestroyAdmin
+ * <li> ServiceDestroyer.DEACTIVATION_TIMEOUT - returned when the
+ * service's activation descriptor is still registered with the
+ * activation system after the number of seconds to wait have passed
+ * <li> ServiceDestroyer.PERSISTENT_STORE_EXISTS - returned when the
+ * directory in which the service stores its persistent state
+ * still exists after the service has been successfully destroyed
+ * </ul>
+ *
+ * @throws java.rmi.RemoteException typically, this exception occurs when
+ * there is a communication failure between the client and the
+ * service's backend. When this exception does occur, the
+ * service may or may not have been successfully destroyed.
+ * @throws java.rmi.activation.ActivationException typically, this
+ * exception occurs when problems arise while attempting to
+ * interact with the activation system
+ */
+ public static int destroyShared(ActivationGroupID gid,
+ ActivationID id,
+ Object proxy,
+ int nSecsWait) throws RemoteException,
+ ActivationException
+ {
+ /* Retrieve the current location of the service's persistent store. */
+ String logDir = null;
+ if(proxy instanceof Administrable) {
+ Object admin = ((Administrable)proxy).getAdmin();
+ if(admin instanceof StorageLocationAdmin) {
+ StorageLocationAdmin storeAdmin = (StorageLocationAdmin)admin;
+ logDir = storeAdmin.getStorageLocation();
+ }//endif
+ }//endif
+ /* Destroy the service */
+ int destroyCode = destroy(proxy);
+ if(destroyCode != DESTROY_SUCCESS) return destroyCode;
+ /* Verify the service has actually been destroyed by waiting until
+ * service's activation ID is no longer registered with the
+ * activation system.
+ *
+ * Since an exception will be thrown when an attempt is made to
+ * retrieve an activation descriptor for an ID which is not
+ * registered, this method makes repeated attempts to retrieve the
+ * activation descriptor until such an exception is thrown,
+ * or until the indicated number of seconds to wait has passed.
+ */
+ boolean deactivated = false;
+ for(int i=0;i<nSecsWait;i++) {
+ try {
+ ActivationGroup.getSystem().getActivationDesc(id);
+ } catch (UnknownObjectException e) {
+ deactivated = true;
+ break;
+ }//end try
+ try {
+ Thread.sleep(1*N_MS_PER_SEC);
+ } catch (InterruptedException e) { }
+ }//end loop
+ if(!deactivated) return DEACTIVATION_TIMEOUT;
+ /* Determine if the service's persistent store was successfully
+ * destroyed.
+ */
+ if(logDir != null) {
+ File logDirFD = new File(logDir);
+ if( logDirFD.exists() && logDirFD.isDirectory() ) {
+ return PERSISTENT_STORE_EXISTS;
+ }
+ }
+ return DESTROY_SUCCESS;
+ }//end destroyShared
+
+}//end class ServiceDestroyer
Propchange: river/tck/src/com/sun/jini/compat/start/ServiceDestroyer.java
------------------------------------------------------------------------------
svn:eol-style = native