View Javadoc

1   /*
2    * Copyright 2010-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.testdoc.core.adapter;
17  
18  import java.lang.management.ManagementFactory;
19  
20  import javax.management.InstanceAlreadyExistsException;
21  import javax.management.JMX;
22  import javax.management.MBeanRegistrationException;
23  import javax.management.MBeanServer;
24  import javax.management.MalformedObjectNameException;
25  import javax.management.NotCompliantMBeanException;
26  import javax.management.ObjectName;
27  
28  import de.smartics.testdoc.core.doc.Type;
29  import de.smartics.testdoc.core.doc.UnitTestDoc;
30  import de.smartics.testdoc.core.export.AbstractExportAdapter;
31  import de.smartics.testdoc.core.export.ExportException;
32  
33  /**
34   * The client to the JMX bean stored at the MBean server. This client is
35   * responsible to register the {@link JmxExportAdapterMBean} and to delegate all
36   * calls to the {@link de.smartics.testdoc.core.export.ExportAdapter} interface to
37   * the MBean instance.
38   *
39   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
40   * @version $Revision:591 $
41   */
42  public class JmxExportAdapterClient extends AbstractExportAdapter
43  {
44    // ********************************* Fields *********************************
45  
46    // --- constants ------------------------------------------------------------
47  
48    /**
49     * The class version identifier.
50     */
51    private static final long serialVersionUID = 1L;
52  
53    /**
54     * The name that registers the instance of {@link JmxExportAdapterMBean}.
55     * <p>
56     * The value of this constant is {@value}.
57     * </p>
58     */
59    public static final String OBJECT_NAME =
60        "de.smartics.testdoc.adapter:type=JmxExportAdapterClient,name=jmxExportAdapter";
61  
62    // --- members --------------------------------------------------------------
63  
64    /**
65     * The JMX object name to retrieve the instance from the server.
66     */
67    private final ObjectName name;
68  
69    /**
70     * The JMX Bean to delegate the export adapter calls to.
71     */
72    private final JmxExportAdapterMBean delegate;
73  
74    // ****************************** Initializer *******************************
75  
76    // ****************************** Constructors ******************************
77  
78    /**
79     * Default constructor.
80     *
81     * @throws MBeanRegistrationException if the registration failed.
82     */
83    public JmxExportAdapterClient() throws MBeanRegistrationException
84    {
85      this.name = createObjectName();
86      this.delegate = createAndRegisterMBeanToPlatformServer();
87    }
88  
89    // ****************************** Inner Classes *****************************
90  
91    // ********************************* Methods ********************************
92  
93    // --- init -----------------------------------------------------------------
94  
95    private static ObjectName createObjectName() throws IllegalStateException
96    {
97      try
98      {
99        return new ObjectName(OBJECT_NAME);
100     }
101     catch (final MalformedObjectNameException e)
102     {
103       throw new IllegalStateException("The object name '" + OBJECT_NAME
104                                       + "' is invalid.", e);
105     }
106   }
107 
108   private JmxExportAdapterMBean createAndRegisterMBeanToPlatformServer()
109     throws MBeanRegistrationException
110   {
111     JmxExportAdapterMBean delegate = new JmxExportAdapter();
112 
113     final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
114     try
115     {
116       server.registerMBean(delegate, name);
117     }
118     catch (final InstanceAlreadyExistsException e)
119     {
120       delegate = fetchExportAdapterMBean(server, name);
121     }
122     catch (final NotCompliantMBeanException e)
123     {
124       throw new IllegalStateException(
125           "The instance to be registered with object name '" + name
126               + "' is not a valid MBean.", e);
127     }
128     return delegate;
129   }
130 
131   private static JmxExportAdapterMBean fetchExportAdapterMBean(
132       final MBeanServer server, final ObjectName name)
133   {
134     return JMX.newMBeanProxy(server, name, JmxExportAdapterMBean.class);
135   }
136 
137   // --- get&set --------------------------------------------------------------
138 
139   /**
140    * Returns the JMX object name to retrieve the instance from the server.
141    *
142    * @return the JMX object name to retrieve the instance from the server.
143    */
144   public ObjectName getName()
145   {
146     return name;
147   }
148 
149   // --- business -------------------------------------------------------------
150 
151   /**
152    * {@inheritDoc}
153    *
154    * @see de.smartics.testdoc.core.export.ExportAdapter#export(de.smartics.testdoc.core.doc.UnitTestDoc)
155    */
156   @Override
157   public void export(final UnitTestDoc testDoc) throws ExportException
158   {
159     delegate.export(testDoc);
160   }
161 
162   /**
163    * {@inheritDoc}
164    *
165    * @see de.smartics.testdoc.core.export.ExportAdapter#clear(de.smartics.testdoc.core.doc.Type)
166    */
167   @Override
168   public void clear(final Type testCaseType)
169   {
170     delegate.clear(testCaseType);
171   }
172 
173   // --- object basics --------------------------------------------------------
174 
175 }