summaryrefslogtreecommitdiffstats
blob: f00cf940c738d2869df987230b59858101f9d8d4 (plain)
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
/*******************************************************************************
 * Copyright (c) 2004, 2007 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.orcs.db.internal.proxy;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.core.util.Conditions;
import org.eclipse.osee.framework.jdk.core.util.Collections;
import org.eclipse.osee.framework.resource.management.IResourceManager;
import org.eclipse.osee.logger.Log;
import org.eclipse.osee.orcs.core.ds.DataProxy;
import org.eclipse.osee.orcs.core.ds.DataProxyFactory;

/**
 * @author Roberto E. Escobar
 */
public class ResourceDataProxyFactory implements DataProxyFactory {

   private final Map<String, Class<? extends AbstractDataProxy>> proxyClassMap =
      new HashMap<String, Class<? extends AbstractDataProxy>>();

   private Log logger;
   private IResourceManager resourceManager;

   public void setLogger(Log logger) {
      this.logger = logger;
   }

   public Log getLogger() {
      return logger;
   }

   public IResourceManager getResourceManager() {
      return resourceManager;
   }

   public void setResourceManager(IResourceManager resourceManager) {
      this.resourceManager = resourceManager;
   }

   private Class<? extends AbstractDataProxy> getProxyClazz(String id) {
      return proxyClassMap.get(id);
   }

   public void start(Map<String, Object> properties) throws Exception {
      Object object = properties.get(PROXY_FACTORY_ALIAS);
      String[] factoryAliases = new String[0];
      if (object instanceof String[]) {
         factoryAliases = (String[]) object;
      }
      Conditions.checkNotNull(factoryAliases, "PROXY_FACTORY_ALIAS");

      proxyClassMap.put("DefaultAttributeDataProvider", VarCharDataProxy.class);
      proxyClassMap.put("ClobAttributeDataProvider", VarCharDataProxy.class);

      proxyClassMap.put("UriAttributeDataProvider", UriDataProxy.class);
      proxyClassMap.put("MappedAttributeDataProvider", UriDataProxy.class);

      List<String> aliases = Arrays.asList(factoryAliases);
      Set<String> keys = proxyClassMap.keySet();

      Conditions.checkExpressionFailOnTrue(!Collections.setComplement(aliases, keys).isEmpty(),
         "Check osgi.ds declaration - property mismatch detected");
      Conditions.checkExpressionFailOnTrue(!Collections.setComplement(keys, aliases).isEmpty(),
         "Check osgi.ds declaration - property mismatch detected");
   }

   public void stop() {
      proxyClassMap.clear();
   }

   @Override
   public DataProxy createInstance(String factoryAlias) throws OseeCoreException {
      Class<? extends AbstractDataProxy> clazz = getProxyClazz(factoryAlias);
      Conditions.checkNotNull(clazz, "DataProxy", "Unable to find data proxy clazz [%s]", factoryAlias);

      AbstractDataProxy dataProxy = null;
      try {
         dataProxy = clazz.newInstance();
      } catch (Exception ex) {
         getLogger().error(ex, "Error creating data proxy for [%s]", factoryAlias);
         OseeExceptions.wrapAndThrow(ex);
      }
      dataProxy.setLogger(getLogger());
      dataProxy.setStorage(createStorage());
      return dataProxy;
   }

   private Storage createStorage() {
      IResourceManager resourceManager = getResourceManager();
      DataHandler handler = new ResourceHandler(resourceManager);
      return new Storage(handler);
   }
}