View Javadoc
1   package fr.ifremer.tutti.service;
2   
3   /*
4    * #%L
5    * Tutti :: Service
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.base.Preconditions;
26  import com.google.common.cache.CacheBuilder;
27  import com.google.common.cache.CacheLoader;
28  import com.google.common.cache.LoadingCache;
29  import fr.ifremer.tutti.TuttiConfiguration;
30  import fr.ifremer.tutti.persistence.entities.data.SampleCategoryModel;
31  import org.apache.commons.io.IOUtils;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.nuiton.jaxx.application.ApplicationTechnicalException;
35  
36  import java.io.Closeable;
37  import java.io.IOException;
38  import java.lang.reflect.Constructor;
39  import java.util.Date;
40  import java.util.UUID;
41  import java.util.concurrent.ExecutionException;
42  
43  import static org.nuiton.i18n.I18n.t;
44  
45  /**
46   * Tutti application context.
47   *
48   * This context is load at startup time and close when application dies.
49   *
50   * @author Tony Chemit - chemit@codelutin.com
51   * @since 0.1
52   */
53  public class TuttiServiceContext implements Closeable {
54  
55      /** Logger. */
56      private static final Log log =
57              LogFactory.getLog(TuttiServiceContext.class);
58  
59      protected final TuttiConfiguration config;
60  
61      protected final TuttiDataContext dataContext;
62  
63      protected final LoadingCache<Class<? extends TuttiService>, TuttiService> services;
64  
65      public TuttiServiceContext(TuttiConfiguration config) {
66          this(config, new TuttiDataContext());
67      }
68  
69      public TuttiServiceContext(TuttiConfiguration config, TuttiDataContext dataContext) {
70          this.config = config;
71          this.dataContext = dataContext;
72          // add datacontext in shared valueStack
73          TuttiValidationDataContextSupport.setValidationContext(dataContext.getValidationContext(), false);
74          this.services = CacheBuilder.newBuilder().build(new CacheLoader<Class<? extends TuttiService>, TuttiService>() {
75              @Override
76              public TuttiService load(Class<? extends TuttiService> key) throws Exception {
77                  Preconditions.checkNotNull(key);
78                  Constructor<? extends TuttiService> constructor = key.getConstructor();
79                  Preconditions.checkNotNull(constructor);
80                  TuttiService service = constructor.newInstance();
81                  if (log.isDebugEnabled()) {
82                      log.debug("New service " + service);
83                  }
84                  service.setServiceContext(TuttiServiceContext.this);
85                  return service;
86              }
87          });
88      }
89  
90      public TuttiConfiguration getConfig() {
91          return config;
92      }
93  
94      public TuttiDataContext getDataContext() {
95          return dataContext;
96      }
97  
98      public Date currentDate() {
99          return new Date();
100     }
101 
102     public <S extends TuttiService> S getService(Class<S> serviceType) {
103         if (serviceType == null) {
104             return null;
105         }
106         try {
107             return (S) services.get(serviceType);
108         } catch (ExecutionException e) {
109             throw new ApplicationTechnicalException(t("tutti.service.context.serviceInstanciation.error", serviceType), e);
110         }
111     }
112 
113     public <S extends TuttiService> S reloadService(Class<S> serviceType) {
114         S service = (S) services.getIfPresent(serviceType);
115         if (service != null) {
116             if (log.isDebugEnabled()) {
117                 log.debug("Close service " + service);
118             }
119             IOUtils.closeQuietly(service);
120         }
121         services.invalidate(serviceType);
122         return getService(serviceType);
123     }
124 
125     @Override
126     public void close() throws IOException {
127 
128         for (TuttiService service : services.asMap().values()) {
129             if (log.isDebugEnabled()) {
130                 log.debug("Close service " + service);
131             }
132             IOUtils.closeQuietly(service);
133         }
134         services.invalidateAll();
135     }
136 
137     public SampleCategoryModel getSampleCategoryModel() {
138         SampleCategoryModel result = getDataContext().getSampleCategoryModel();
139         Preconditions.checkNotNull(result, "Need a not null sample category model");
140         return result;
141     }
142 
143     public String generateId(Class<?> type) {
144         return UUID.randomUUID().toString();
145     }
146 }