KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > gs > space > GSSpaceFactory


1 /*
2  * $Id: GSSpaceFactory.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.gs.space;
12
13 import net.jini.core.entry.Entry;
14 import net.jini.core.lease.Lease;
15 import net.jini.core.transaction.server.TransactionManager;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.mule.config.i18n.Message;
20 import org.mule.config.i18n.Messages;
21 import org.mule.impl.space.CreateSpaceException;
22 import org.mule.providers.gs.GSConnector;
23 import org.mule.providers.gs.JiniMessage;
24 import org.mule.providers.gs.JiniTransactionFactory;
25 import org.mule.providers.gs.filters.JavaSpaceTemplateFilter;
26 import org.mule.providers.space.SpaceConstants;
27 import org.mule.umo.UMOFilter;
28 import org.mule.umo.endpoint.UMOEndpoint;
29 import org.mule.umo.endpoint.UMOImmutableEndpoint;
30 import org.mule.umo.space.UMOSpace;
31 import org.mule.umo.space.UMOSpaceException;
32 import org.mule.umo.space.UMOSpaceFactory;
33 import org.mule.util.MapUtils;
34
35 import com.j_spaces.core.IJSpace;
36 import com.j_spaces.core.client.FinderException;
37 import com.j_spaces.core.client.LocalTransactionManager;
38
39 /**
40  * Creates a GigaSpaces JavaSpace
41  *
42  * @see GSSpace
43  * @see net.jini.space.JavaSpace
44  */

45 public class GSSpaceFactory implements UMOSpaceFactory
46 {
47     /**
48      * logger used by this class
49      */

50     protected transient Log logger = LogFactory.getLog(getClass());
51
52     private boolean enableMonitorEvents = true;
53
54     public UMOSpace create(String JavaDoc spaceIdentifier) throws UMOSpaceException
55     {
56         if (spaceIdentifier == null)
57         {
58             throw new NullPointerException JavaDoc(new Message(Messages.X_IS_NULL, "spaceIdentifier").toString());
59         }
60
61         try
62         {
63             GSSpace space = new GSSpace(spaceIdentifier, enableMonitorEvents);
64             space.setEntryTemplate(createDefaultEntry(spaceIdentifier));
65             return space;
66         }
67         catch (FinderException e)
68         {
69             throw new CreateSpaceException(e);
70         }
71     }
72
73     /**
74      * Creates a space based on the endpoint URI and can use additional properties,
75      * transaction info, security info and filters associated with the endpoint
76      *
77      * @param endpoint the endpoint from which to construct the space
78      * @return an new Space object
79      * @throws org.mule.umo.space.UMOSpaceException
80      */

81     public UMOSpace create(UMOImmutableEndpoint endpoint) throws UMOSpaceException
82     {
83         try
84         {
85             // retrieve the lease value from the endpoint or use a default
86
long leaseValue = MapUtils.getLongValue(endpoint.getProperties(),
87                 SpaceConstants.SPACE_LEASE_PROPERTY, Lease.FOREVER);
88
89             // TODO the Spaces themselves are keyed on endpoint + any filtering
90
// information. Should that info also be included in the space name??
91
GSSpace space = new GSSpace(endpoint.getEndpointURI().toString(), enableMonitorEvents, leaseValue);
92
93             // Because Jini uses its own transaction management we need to set the
94
// Manager on the
95
// Transaction Factory
96
if (endpoint.getTransactionConfig().getFactory() != null)
97             {
98                 JiniTransactionFactory txFactory = (JiniTransactionFactory)endpoint.getTransactionConfig()
99                     .getFactory();
100                 TransactionManager transactionManager = LocalTransactionManager.getInstance((IJSpace)space.getJavaSpace());
101                 txFactory.setTransactionManager(transactionManager);
102                 txFactory.setTransactionTimeout(((GSConnector)endpoint.getConnector()).getTransactionTimeout());
103                 space.setTransactionFactory(txFactory);
104             }
105
106             // We do not need to set an entry template if we are not recieving
107
// messages from the space
108
if (!endpoint.getType().equalsIgnoreCase(UMOEndpoint.ENDPOINT_TYPE_SENDER))
109             {
110                 // Now set the Entry template on the space
111
if (endpoint.getFilter() != null)
112                 {
113                     UMOFilter filter = endpoint.getFilter();
114                     if (filter instanceof JavaSpaceTemplateFilter)
115                     {
116                         space.setEntryTemplate(((JavaSpaceTemplateFilter)filter).getEntry());
117                     }
118                     else
119                     {
120                         if (!endpoint.getType().equals(UMOImmutableEndpoint.ENDPOINT_TYPE_SENDER))
121                         {
122                             logger.warn("Filter on endpoint "
123                                         + endpoint.getEndpointURI().toString()
124                                         + " Was not a JiniEntryFilter. Endpoint will match all entries of all types for this endpoint");
125                         }
126                         space.setEntryTemplate(createDefaultEntry(endpoint.getEndpointURI().toString()));
127                     }
128                 }
129                 else
130                 {
131                     if (!endpoint.getType().equals(UMOImmutableEndpoint.ENDPOINT_TYPE_SENDER))
132                     {
133                         logger.warn("Filter on endpoint "
134                                     + endpoint.getEndpointURI().toString()
135                                     + " Was not set. Endpoint will match all entries of all types for this endpoint");
136                     }
137                     space.setEntryTemplate(createDefaultEntry(endpoint.getEndpointURI().toString()));
138                 }
139             }
140             return space;
141         }
142         catch (Exception JavaDoc e)
143         {
144             throw new CreateSpaceException(e);
145         }
146     }
147
148     protected Entry createDefaultEntry(String JavaDoc identifier)
149     {
150         // return new ExternalEntry(identifier, null);
151
return new JiniMessage();
152     }
153 }
154
Popular Tags