Skip to main content
summaryrefslogtreecommitdiffstats
blob: e2e42efc812ad2034a96a0be8fed759a78f0c3f7 (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
/*******************************************************************************
 * Copyright (c) 2014 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.jaxrs.server.internal.applications;

import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.ws.rs.core.Application;
import org.eclipse.osee.jaxrs.server.internal.JaxRsVisitable;
import org.eclipse.osee.jaxrs.server.internal.JaxRsVisitor;
import org.osgi.framework.Bundle;

/**
 * @author Roberto E. Escobar
 */
public abstract class AbstractJaxRsApplicationContainer implements JaxRsVisitable {

   private final Map<String, JaxRsApplicationEntry> applications =
      new ConcurrentHashMap<String, JaxRsApplicationEntry>();

   private final String applicationContext;

   public AbstractJaxRsApplicationContainer(String applicationContext) {
      super();
      this.applicationContext = applicationContext;
   }

   public String getApplicationContext() {
      return applicationContext;
   }

   public void add(String key, Bundle bundle, Application application) {
      JaxRsApplicationEntry entry = new JaxRsApplicationEntry(bundle, application);
      applications.put(key, entry);
   }

   public void remove(String key) {
      applications.remove(key);
   }

   public boolean isEmpty() {
      return applications.isEmpty();
   }

   public int size() {
      return applications.size();
   }

   @Override
   public void accept(JaxRsVisitor visitor) {
      visitor.onStartApplicationContainer(applicationContext, size());
      try {
         for (Entry<String, JaxRsApplicationEntry> entry : applications.entrySet()) {
            String componentName = entry.getKey();
            JaxRsApplicationEntry value = entry.getValue();
            Bundle bundle = value.getBundle();
            Application application = value.getApplication();
            visitor.onApplication(applicationContext, componentName, bundle, application);
         }
      } finally {
         visitor.onEndApplicationContainer();
      }
   }

   protected Application getApplication() {
      Application toReturn = null;
      if (applications.size() > 1) {
         toReturn = newCompositeApplication();
      } else {
         JaxRsApplicationEntry entry = getFirstEntry();
         toReturn = entry != null ? entry.getApplication() : null;
      }
      return toReturn;
   }

   private JaxRsApplicationEntry getFirstEntry() {
      return !applications.isEmpty() ? applications.values().iterator().next() : null;
   }

   private final Application newCompositeApplication() {
      final Set<Class<?>> classes = new LinkedHashSet<>();
      final Set<Object> singletons = new LinkedHashSet<>();
      for (JaxRsApplicationEntry appEntry : applications.values()) {
         Application application = appEntry.getApplication();
         classes.addAll(application.getClasses());
         singletons.addAll(application.getSingletons());
      }
      return new Application() {
         @Override
         public Set<Class<?>> getClasses() {
            return classes;
         }

         @Override
         public Set<Object> getSingletons() {
            return singletons;
         }
      };
   }

   @Override
   public String toString() {
      return " applicationContext=" + applicationContext;
   }

   private final class JaxRsApplicationEntry {
      private final Bundle bundle;
      private final Application application;

      public JaxRsApplicationEntry(Bundle bundle, Application application) {
         super();
         this.bundle = bundle;
         this.application = application;
      }

      public Bundle getBundle() {
         return bundle;
      }

      public Application getApplication() {
         return application;
      }
   }
}

Back to the top