View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.jca;
8   
9   import org.mule.api.endpoint.EndpointException;
10  import org.mule.api.endpoint.EndpointURI;
11  import org.mule.endpoint.MuleEndpointURI;
12  import org.mule.util.StringUtils;
13  
14  import java.io.Serializable;
15  import java.util.Properties;
16  
17  import javax.resource.ResourceException;
18  import javax.resource.spi.ActivationSpec;
19  import javax.resource.spi.InvalidPropertyException;
20  import javax.resource.spi.ResourceAdapter;
21  
22  /**
23   * <code>MuleActivationSpec</code> defines the contract between a Message Driven
24   * Bean (MDB) and the Mule Resource Adapter. This spec holds the configuration values
25   * used to register inbound communication with the Resource Adapter
26   */
27  public class MuleActivationSpec implements ActivationSpec, Serializable
28  {
29      /**
30       * Serial version
31       */
32      private static final long serialVersionUID = 735353511874563914L;
33  
34      private Properties propertiesMap;
35      private String endpointName;
36      private String connectorName;
37      private int createConnector;
38      private MuleResourceAdapter resourceAdapter;
39      private String endpoint;
40      private EndpointURI endpointURI;
41      private String modelName;
42  
43      public Properties getPropertiesMap()
44      {
45          return propertiesMap;
46      }
47  
48      public void setPropertiesMap(Properties propertiesMap)
49      {
50          this.propertiesMap = propertiesMap;
51      }
52  
53      public void setPropertiesMap(String properties)
54      {
55          String[] pairs = StringUtils.splitAndTrim(properties, ",");
56          Properties props = new Properties();
57  
58          for (int i = 0; i < pairs.length; i++)
59          {
60              String pair = pairs[i];
61              int x = pair.indexOf('=');
62              if (x == -1)
63              {
64                  props.setProperty(pair, null);
65              }
66              else
67              {
68                  props.setProperty(pair.substring(0, x), pair.substring(x + 1));
69              }
70          }
71  
72          this.setPropertiesMap(props);
73      }
74  
75      public String getEndpointName()
76      {
77          return endpointName;
78      }
79  
80      public void setEndpointName(String endpointName)
81      {
82          this.endpointName = endpointName;
83      }
84  
85      public String getConnectorName()
86      {
87          return connectorName;
88      }
89  
90      public void setConnectorName(String connectorName)
91      {
92          this.connectorName = connectorName;
93      }
94  
95      public int getCreateConnector()
96      {
97          return createConnector;
98      }
99  
100     public void setCreateConnector(int createConnector)
101     {
102         this.createConnector = createConnector;
103     }
104 
105     public String getEndpoint()
106     {
107         return endpoint;
108     }
109 
110     public void setEndpoint(String endpoint)
111     {
112         this.endpoint = endpoint;
113 
114     }
115 
116     public void validate() throws InvalidPropertyException
117     {
118         try
119         {
120             this.endpointURI = new MuleEndpointURI(endpoint, resourceAdapter.muleContext);
121         }
122         catch (EndpointException e)
123         {
124             throw new InvalidPropertyException(e);
125         }
126 
127         if (propertiesMap != null)
128         {
129             propertiesMap.putAll(this.endpointURI.getParams());
130         }
131         else
132         {
133             propertiesMap = this.endpointURI.getParams();
134         }
135         if (endpoint == null)
136         {
137             throw new InvalidPropertyException("endpoint is null");
138         }
139 
140         if (endpointURI == null)
141         {
142             throw new InvalidPropertyException("endpointURI is null");
143         }
144     }
145 
146     public ResourceAdapter getResourceAdapter()
147     {
148         return resourceAdapter;
149     }
150 
151     public void setResourceAdapter(ResourceAdapter resourceAdapter) throws ResourceException
152     {
153         // spec section 5.3.3
154         if (this.resourceAdapter != null)
155         {
156             throw new ResourceException("ResourceAdapter already set");
157         }
158         if (!(resourceAdapter instanceof MuleResourceAdapter))
159         {
160             throw new ResourceException("ResourceAdapter is not of type: "
161                                         + MuleResourceAdapter.class.getName());
162         }
163         this.resourceAdapter = (MuleResourceAdapter)resourceAdapter;
164     }
165 
166     public String getModelName() {
167         return modelName;
168     }
169 
170     public void setModelName(String modelName) {
171         this.modelName = modelName;
172     }
173 }