001
014
015 package com.liferay.portal.struts;
016
017 import com.liferay.portal.kernel.struts.StrutsAction;
018 import com.liferay.portal.kernel.struts.StrutsPortletAction;
019 import com.liferay.registry.Filter;
020 import com.liferay.registry.Registry;
021 import com.liferay.registry.RegistryUtil;
022 import com.liferay.registry.ServiceReference;
023 import com.liferay.registry.ServiceRegistration;
024 import com.liferay.registry.ServiceTracker;
025 import com.liferay.registry.ServiceTrackerCustomizer;
026 import com.liferay.registry.collections.StringServiceRegistrationMap;
027
028 import java.util.HashMap;
029 import java.util.Map;
030 import java.util.concurrent.ConcurrentHashMap;
031
032 import org.apache.struts.action.Action;
033
034
038 public class StrutsActionRegistryUtil {
039
040 public static Action getAction(String path) {
041 return _instance._getAction(path);
042 }
043
044 public static Map<String, Action> getActions() {
045 return _instance._getActions();
046 }
047
048 public static void register(String path, StrutsAction strutsAction) {
049 _instance._register(path, strutsAction);
050 }
051
052 public static void register(
053 String path, StrutsPortletAction strutsPortletAction) {
054
055 _instance._register(path, strutsPortletAction);
056 }
057
058 public static void unregister(String path) {
059 _instance._unregister(path);
060 }
061
062 private StrutsActionRegistryUtil() {
063 Registry registry = RegistryUtil.getRegistry();
064
065 Filter filter = registry.getFilter(
066 "(&(|(objectClass=" + StrutsAction.class.getName() +
067 ")(objectClass=" + StrutsPortletAction.class.getName() +
068 "))(path=*))");
069
070 _serviceTracker = registry.trackServices(
071 filter, new ActionServiceTrackerCustomizer());
072
073 _serviceTracker.open();
074 }
075
076 private Action _getAction(String path) {
077 Action action = _actions.get(path);
078
079 if (action != null) {
080 return action;
081 }
082
083 for (Map.Entry<String, Action> entry : _actions.entrySet()) {
084 if (path.startsWith(entry.getKey())) {
085 return entry.getValue();
086 }
087 }
088
089 return null;
090 }
091
092 private Map<String, Action> _getActions() {
093 return _actions;
094 }
095
096 private void _register(String path, StrutsAction strutsAction) {
097 Registry registry = RegistryUtil.getRegistry();
098
099 Map<String, Object> properties = new HashMap<String, Object>();
100
101 properties.put("path", path);
102
103 ServiceRegistration<StrutsAction> serviceRegistration =
104 registry.registerService(
105 StrutsAction.class, strutsAction, properties);
106
107 _strutsActionServiceRegistrations.put(path, serviceRegistration);
108 }
109
110 private void _register(
111 String path, StrutsPortletAction strutsPortletAction) {
112
113 Registry registry = RegistryUtil.getRegistry();
114
115 Map<String, Object> properties = new HashMap<String, Object>();
116
117 properties.put("path", path);
118
119 ServiceRegistration<StrutsPortletAction> serviceRegistration =
120 registry.registerService(
121 StrutsPortletAction.class, strutsPortletAction, properties);
122
123 _strutsPortletActionServiceRegistrations.put(path, serviceRegistration);
124 }
125
126 private void _unregister(String path) {
127 ServiceRegistration<?> serviceRegistration =
128 _strutsActionServiceRegistrations.remove(path);
129
130 if (serviceRegistration != null) {
131 serviceRegistration.unregister();
132 }
133
134 serviceRegistration = _strutsPortletActionServiceRegistrations.remove(
135 path);
136
137 if (serviceRegistration != null) {
138 serviceRegistration.unregister();
139 }
140 }
141
142 private static final StrutsActionRegistryUtil _instance =
143 new StrutsActionRegistryUtil();
144
145 private final Map<String, Action> _actions =
146 new ConcurrentHashMap<String, Action>();
147 private final ServiceTracker<?, Action> _serviceTracker;
148 private final StringServiceRegistrationMap<StrutsAction>
149 _strutsActionServiceRegistrations =
150 new StringServiceRegistrationMap<StrutsAction>();
151 private final StringServiceRegistrationMap<StrutsPortletAction>
152 _strutsPortletActionServiceRegistrations =
153 new StringServiceRegistrationMap<StrutsPortletAction>();
154
155 private class ActionServiceTrackerCustomizer
156 implements ServiceTrackerCustomizer<Object, Action> {
157
158 @Override
159 public Action addingService(ServiceReference<Object> serviceReference) {
160 Registry registry = RegistryUtil.getRegistry();
161
162 Object service = registry.getService(serviceReference);
163
164 Action action = null;
165
166 if (service instanceof StrutsAction) {
167 action = new ActionAdapter((StrutsAction)service);
168 }
169 else if (service instanceof StrutsPortletAction) {
170 action = new PortletActionAdapter((StrutsPortletAction)service);
171 }
172
173 String path = (String)serviceReference.getProperty("path");
174
175 _actions.put(path, action);
176
177 return action;
178 }
179
180 @Override
181 public void modifiedService(
182 ServiceReference<Object> serviceReference, Action service) {
183 }
184
185 @Override
186 public void removedService(
187 ServiceReference<Object> serviceReference, Action service) {
188
189 Registry registry = RegistryUtil.getRegistry();
190
191 registry.ungetService(serviceReference);
192
193 String path = (String)serviceReference.getProperty("path");
194
195 _actions.remove(path);
196 }
197
198 }
199
200 }