View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    * 
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.river.tool.envcheck.plugins;
19  
20  import org.apache.river.tool.envcheck.AbstractPlugin;
21  import org.apache.river.tool.envcheck.EnvCheck;
22  import org.apache.river.tool.envcheck.Reporter;
23  import org.apache.river.tool.envcheck.Reporter.Message;
24  import org.apache.river.tool.envcheck.SubVMTask;
25  import java.rmi.activation.ActivationGroup;
26  import java.security.AccessControlException;
27  
28  /**
29   * Plugin which checks the liveness of the activation system. The
30   * check will be performed if either:
31   *
32   * <ul>
33   * <li>the <code>Configuration</code> for the tool includes an entry
34   *     named <code>activation</code> which is a boolean and has a
35   *     value of <code>true</code>.
36   * <li>a service starter configuration is being checked and it contains
37   *     an entry of type <code>SharedActivatableServiceDescriptor</code>.
38   * </ul>
39   * The check is performed by calling <code>ActivationGroup.getSystem()</code>.
40   * A non-exceptional return indicates liveness.
41   */
42  public class CheckActivationSystem extends AbstractPlugin {
43  
44      /** reference to the container */
45      private EnvCheck envCheck;
46  
47      /** flag indicating whether to perform the check */
48      private static boolean doCheck = false;
49  
50      /**
51       * Test whether to unconditionally test for the presence of the
52       * activation system. This will be the case if <code>opt</code> is
53       * the command-line option <code>-activation</code>.
54       *
55       * @param opt the command-line option
56       * @return true if the option is recognized by this plugin
57       */
58      public boolean isPluginOption(String opt) {
59  	if (opt.equals("-activation")) {
60  	    doCheck = true;
61  	    return true;
62  	}
63  	return false;
64      }
65  
66      /**
67       * Determine whether to perform this check, and perform the 
68       * check if appropriate.
69       */
70      // XXX add support for non-default host/port values
71      public void run(EnvCheck envCheck) {
72  	this.envCheck = envCheck;
73  	if (doCheck || envCheck.getGroupDescriptor() != null) {
74  	    checkAvailability();
75  	}
76      }
77  
78      /**
79       * Performs the check 
80       */
81      private void checkAvailability() {
82  	Message message;
83  	String taskName = taskName("CheckActivationTask");
84  	// OK if envCheck.getGroupDescriptor() returns null
85  	Object launchReturn = envCheck.launch(null, 
86  					      envCheck.getGroupDescriptor(),
87  					      taskName);
88  	if (launchReturn == null) {
89  	    message = new Message(Reporter.INFO,
90  				  getString("running"),
91  				  getString("explanationString"));
92  	} else {
93  	    Throwable ex = (Throwable) launchReturn;
94  	    Throwable t = ex.getCause();
95  	    if (t == null) {
96  		t = ex;
97  	    }
98  	    if (t instanceof java.rmi.ConnectException) {
99  		message = new Message(Reporter.WARNING,
100 				      getString("notRunning"),
101 				      t,
102 				      getString("explanationString"));
103 	    } else if (t instanceof AccessControlException) {
104 		message = new Message(Reporter.WARNING,
105 				      getString("nopermission"),
106 				      t,
107 				      getString("noPermExplanationString"));
108 	    } else {
109 		message = new Message(Reporter.WARNING,
110 				      getString("unexpectedException"),
111 				      ex,
112 				      getString("unexpectedExplanationString"));
113 	    }
114 	}
115 	Reporter.print(message);
116     }
117 
118     /** subtask to perform the actual check */
119     public static class CheckActivationTask implements SubVMTask {
120 	
121 	public Object run(String[] args) {
122 //          don't make the test sensitive to policy settings for now
123 //  	    System.setSecurityManager(new RMISecurityManager());
124 	    try {
125 		ActivationGroup.getSystem();
126 		return null;
127 	    } catch (Throwable e) {
128 		return e;
129 	    }
130 	}
131     }
132 }