001
014
015 package com.liferay.portal.struts;
016
017 import com.liferay.portal.kernel.concurrent.ConcurrentHashSet;
018 import com.liferay.registry.Registry;
019 import com.liferay.registry.RegistryUtil;
020 import com.liferay.registry.ServiceReference;
021 import com.liferay.registry.ServiceRegistration;
022 import com.liferay.registry.ServiceTracker;
023 import com.liferay.registry.ServiceTrackerCustomizer;
024 import com.liferay.registry.collections.StringServiceRegistrationMap;
025 import com.liferay.registry.collections.StringServiceRegistrationMapImpl;
026 import com.liferay.registry.util.StringPlus;
027
028 import java.util.HashMap;
029 import java.util.List;
030 import java.util.Map;
031 import java.util.Set;
032
033
037 public class AuthPublicPathRegistry {
038
039 public static boolean contains(String path) {
040 return _instance._contains(path);
041 }
042
043 public static void register(String... paths) {
044 _instance._register(paths);
045 }
046
047 public static void unregister(String... paths) {
048 _instance._unregister(paths);
049 }
050
051 private AuthPublicPathRegistry() {
052 Registry registry = RegistryUtil.getRegistry();
053
054 _serviceTracker = registry.trackServices(
055 registry.getFilter(
056 "(&(auth.public.path=*)(objectClass=java.lang.Object))"),
057 new AuthPublicTrackerCustomizer());
058
059 _serviceTracker.open();
060 }
061
062 private boolean _contains(String path) {
063 return _paths.contains(path);
064 }
065
066 private void _register(String... paths) {
067 Registry registry = RegistryUtil.getRegistry();
068
069 for (String path : paths) {
070 Map<String, Object> properties = new HashMap<>();
071
072 properties.put("auth.public.path", path);
073 properties.put("objectClass", Object.class.getName());
074
075 ServiceRegistration<Object> serviceRegistration =
076 registry.registerService(
077 Object.class, new Object(), properties);
078
079 _serviceRegistrations.put(path, serviceRegistration);
080 }
081 }
082
083 private void _unregister(String... paths) {
084 for (String path : paths) {
085 ServiceRegistration<Object> serviceRegistration =
086 _serviceRegistrations.remove(path);
087
088 if (serviceRegistration != null) {
089 serviceRegistration.unregister();
090 }
091 }
092 }
093
094 private static final AuthPublicPathRegistry _instance =
095 new AuthPublicPathRegistry();
096
097 private final Set<String> _paths = new ConcurrentHashSet<>();
098 private final StringServiceRegistrationMap<Object>
099 _serviceRegistrations = new StringServiceRegistrationMapImpl<>();
100 private final ServiceTracker<Object, Object> _serviceTracker;
101
102 private class AuthPublicTrackerCustomizer
103 implements ServiceTrackerCustomizer<Object, Object> {
104
105 @Override
106 public Object addingService(ServiceReference<Object> serviceReference) {
107 List<String> paths = StringPlus.asList(
108 serviceReference.getProperty("auth.public.path"));
109
110 for (String path : paths) {
111 _paths.add(path);
112 }
113
114 Registry registry = RegistryUtil.getRegistry();
115
116 return registry.getService(serviceReference);
117 }
118
119 @Override
120 public void modifiedService(
121 ServiceReference<Object> serviceReference, Object object) {
122 }
123
124 @Override
125 public void removedService(
126 ServiceReference<Object> serviceReference, Object object) {
127
128 List<String> paths = StringPlus.asList(
129 serviceReference.getProperty("auth.public.path"));
130
131 for (String path : paths) {
132 _paths.remove(path);
133 }
134 }
135
136 }
137
138 }