Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3915f7a723847583a72654820e58f20881aa8073 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*******************************************************************************
 * Copyright (c) 2010 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.access.internal;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import org.eclipse.osee.framework.access.IAccessProvider;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.enums.PermissionEnum;
import org.eclipse.osee.framework.core.model.IBasicArtifact;
import org.eclipse.osee.framework.core.model.access.AccessDataQuery;
import org.eclipse.osee.framework.core.services.IAccessControlService;
import org.eclipse.osee.framework.core.services.IOseeCachingService;
import org.eclipse.osee.framework.core.services.IdentityService;
import org.eclipse.osee.framework.core.util.Conditions;
import org.eclipse.osee.framework.database.IOseeDatabaseService;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.lifecycle.ILifecycleService;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.event.OseeEventService;
import org.eclipse.osee.framework.skynet.core.event.listener.EventQosType;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

public final class AccessControlServiceProxy implements IAccessControlService {

   private final List<ServiceReference<IAccessProvider>> registered =
      new CopyOnWriteArrayList<ServiceReference<IAccessProvider>>();

   private final List<ServiceReference<IAccessProvider>> pendingProviders =
      new CopyOnWriteArrayList<ServiceReference<IAccessProvider>>();

   private IOseeDatabaseService dbService;
   private IOseeCachingService cachingService;
   private IdentityService identityService;
   private OseeEventService eventService;
   private ILifecycleService lifecycleService;

   private AccessControlService accessService;
   private AccessEventListener accessEventListener;
   private Thread thread;

   public void setDbService(IOseeDatabaseService dbService) {
      this.dbService = dbService;
   }

   public void setCachingService(IOseeCachingService cachingService) {
      this.cachingService = cachingService;
   }

   public void setIdentityService(IdentityService identityService) {
      this.identityService = identityService;
   }

   public void setEventService(OseeEventService eventService) {
      this.eventService = eventService;
   }

   public void setLifecycleService(ILifecycleService service) {
      this.lifecycleService = service;
   }

   public void addAccessProvider(ServiceReference<IAccessProvider> reference) {
      if (isReady()) {
         register(reference);
         registered.add(reference);
      } else {
         pendingProviders.add(reference);
      }
   }

   public void removeAccessProvider(ServiceReference<IAccessProvider> reference) {
      if (isReady()) {
         deregister(reference);
         registered.remove(reference);
      } else {
         pendingProviders.remove(reference);
      }
   }

   private IAccessProvider getService(ServiceReference<IAccessProvider> reference) {
      Bundle bundle = reference.getBundle();
      BundleContext bundleContext = bundle.getBundleContext();
      return bundleContext.getService(reference);
   }

   private void register(ServiceReference<IAccessProvider> reference) {
      try {
         IAccessProvider accessProvider = getService(reference);
         lifecycleService.addHandler(AccessProviderVisitor.TYPE, accessProvider);
      } catch (OseeCoreException ex) {
         OseeLog.log(AccessControlHelper.class, Level.SEVERE, ex);
      }
   }

   private void deregister(ServiceReference<IAccessProvider> reference) {
      try {
         IAccessProvider accessProvider = getService(reference);
         lifecycleService.removeHandler(AccessProviderVisitor.TYPE, accessProvider);
      } catch (OseeCoreException ex) {
         OseeLog.log(AccessControlHelper.class, Level.SEVERE, ex);
      }
   }

   private synchronized void ensureInitialized() {
      if (accessService != null && !pendingProviders.isEmpty()) {
         for (ServiceReference<IAccessProvider> reference : pendingProviders) {
            register(reference);
         }
         pendingProviders.clear();
      }
   }

   public AccessControlService getProxiedObject() {
      ensureInitialized();
      return accessService;
   }

   public void reloadCache() {
      try {
         getProxiedObject().reloadCache();
      } catch (OseeCoreException ex) {
         OseeLog.log(AccessControlServiceProxy.class, Level.WARNING, ex);
      }
   }

   private boolean isReady() {
      return accessService != null && lifecycleService != null;
   }

   public void start() {
      accessService = new AccessControlService(dbService, cachingService, identityService, eventService);

      accessEventListener = new AccessEventListener(accessService, new AccessControlCacheHandler());
      eventService.addListener(EventQosType.PRIORITY, accessEventListener);
      accessService.start();
   }

   public void stop() {
      if (thread != null) {
         thread.interrupt();
         thread = null;
      }

      for (ServiceReference<IAccessProvider> provider : registered) {
         deregister(provider);
      }
      registered.clear();

      if (accessEventListener != null) {
         eventService.removeListener(EventQosType.PRIORITY, accessEventListener);
         accessEventListener = null;
      }

      if (accessService != null) {
         accessService.stop();
         accessService = null;
      }
   }

   private void checkInitialized() throws OseeCoreException {
      Conditions.checkNotNull(getProxiedObject(), "accessService", "Access Service not properly initialized");
   }

   @Override
   public boolean hasPermission(Object object, PermissionEnum permission) throws OseeCoreException {
      checkInitialized();
      return getProxiedObject().hasPermission(object, permission);
   }

   @Override
   public void removePermissions(IOseeBranch branch) throws OseeCoreException {
      checkInitialized();
      getProxiedObject().removePermissions(branch);
   }

   @Override
   public AccessDataQuery getAccessData(IBasicArtifact<?> userArtifact, Collection<?> itemsToCheck) throws OseeCoreException {
      checkInitialized();
      return getProxiedObject().getAccessData(userArtifact, itemsToCheck);
   }
}

Back to the top