KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > space > SpaceConnector


1 /*
2  * $Id: SpaceConnector.java 3865 2006-11-09 17:11:08Z Lajos $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.space;
12
13 import org.apache.commons.lang.StringUtils;
14 import org.mule.config.i18n.Message;
15 import org.mule.config.i18n.Messages;
16 import org.mule.providers.AbstractServiceEnabledConnector;
17 import org.mule.umo.UMOComponent;
18 import org.mule.umo.endpoint.UMOEndpoint;
19 import org.mule.umo.endpoint.UMOImmutableEndpoint;
20 import org.mule.umo.lifecycle.InitialisationException;
21 import org.mule.umo.space.UMOSpace;
22 import org.mule.umo.space.UMOSpaceException;
23 import org.mule.umo.space.UMOSpaceFactory;
24 import org.mule.util.BeanUtils;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /**
31  * Provides generic connectivity to 'Spaces' that implement the Mule Space API, i.e.
32  * Gigaspaces, JCache implementations, Rio can be accessed as well as a mule file,
33  * Journal or VM space.
34  */

35 public class SpaceConnector extends AbstractServiceEnabledConnector
36 {
37     private UMOSpaceFactory spaceFactory;
38     private Map JavaDoc spaceProperties;
39     // TODO Mule 2.0: these are stored on the UMOManagementContext
40
private Map JavaDoc spaces = new HashMap JavaDoc();
41
42     public SpaceConnector()
43     {
44         super();
45     }
46
47     public String JavaDoc getProtocol()
48     {
49         return "space";
50     }
51
52     public void doInitialise() throws InitialisationException
53     {
54         super.doInitialise();
55         if (spaceFactory == null)
56         {
57             throw new InitialisationException(new Message(Messages.X_IS_NULL, "spaceFactory"), this);
58         }
59         if (spaceProperties != null)
60         {
61             BeanUtils.populateWithoutFail(spaceFactory, spaceProperties, true);
62         }
63
64     }
65
66     public UMOSpace getSpace(String JavaDoc spaceUrl) throws UMOSpaceException
67     {
68         logger.info("looking for space: " + spaceUrl);
69         UMOSpace space = (UMOSpace)spaces.get(spaceUrl);
70         if (space == null)
71         {
72             logger.info("Space not found, creating space: " + spaceUrl);
73             space = spaceFactory.create(spaceUrl);
74             spaces.put(spaceUrl, space);
75         }
76         return space;
77     }
78
79     /**
80      * Will look up a space based on the URI. If the Space is created this method
81      * will honour the transaction information on the endpoint and set the space up
82      * accordingly
83      *
84      * @param endpoint
85      * @return
86      * @throws UMOSpaceException
87      */

88     public UMOSpace getSpace(UMOImmutableEndpoint endpoint) throws UMOSpaceException
89     {
90         String JavaDoc spaceKey = getSpaceKey(endpoint);
91         logger.info("looking for space: " + spaceKey);
92         UMOSpace space = (UMOSpace)spaces.get(spaceKey);
93         if (space == null)
94         {
95             logger.info("Space not found, creating space: " + spaceKey);
96             space = spaceFactory.create(endpoint);
97             spaces.put(spaceKey, space);
98             if (endpoint.getTransactionConfig().getFactory() != null)
99             {
100                 space.setTransactionFactory(endpoint.getTransactionConfig().getFactory());
101             }
102         }
103         return space;
104     }
105
106     protected String JavaDoc getSpaceKey(UMOImmutableEndpoint endpoint)
107     {
108         return endpoint.getEndpointURI().toString();
109     }
110
111     public UMOSpaceFactory getSpaceFactory()
112     {
113         return spaceFactory;
114     }
115
116     public void setSpaceFactory(UMOSpaceFactory spaceFactory)
117     {
118         this.spaceFactory = spaceFactory;
119     }
120
121     public Map JavaDoc getSpaceProperties()
122     {
123         return spaceProperties;
124     }
125
126     public void setSpaceProperties(Map JavaDoc spaceProperties)
127     {
128         this.spaceProperties = spaceProperties;
129     }
130
131     /**
132      * Template method to perform any work when destroying the connectoe
133      */

134     protected void doDispose()
135     {
136         for (Iterator JavaDoc iterator = spaces.values().iterator(); iterator.hasNext();)
137         {
138             UMOSpace space = (UMOSpace)iterator.next();
139             space.dispose();
140         }
141         spaces.clear();
142     }
143
144     /**
145      * The method determines the key used to store the receiver against.
146      *
147      * @param component the component for which the endpoint is being registered
148      * @param endpoint the endpoint being registered for the component
149      * @return the key to store the newly created receiver against
150      */

151     protected Object JavaDoc getReceiverKey(UMOComponent component, UMOEndpoint endpoint)
152     {
153         return super.getReceiverKey(component, endpoint)
154                + (endpoint.getFilter() != null ? '#' + endpoint.getFilter().toString() : StringUtils.EMPTY);
155     }
156 }
157
Popular Tags