View Javadoc

1   package org.csc.phynixx.loggersystem.logger;
2   
3   /*
4    * #%L
5    * phynixx-common
6    * %%
7    * Copyright (C) 2014 csc
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  
24  import org.csc.phynixx.common.exceptions.DelegatedRuntimeException;
25  import org.csc.phynixx.common.logger.IPhynixxLogger;
26  import org.csc.phynixx.common.logger.PhynixxLogManager;
27  import org.csc.phynixx.loggersystem.logger.channellogger.AccessMode;
28  
29  import java.io.IOException;
30  import java.util.*;
31  
32  /**
33   * the current class is responsible for instanciating new {@link org.csc.phynixx.loggersystem.logger.IDataLogger} and managing their lifecycle. It use a {@link org.csc.phynixx.loggersystem.logger.IDataLoggerFactory} for creating new Logger.
34   *
35   *
36   * the current class managed the lifecycle instances of IDataLoggers.
37   * <p/>
38   * The current implementation do not re-use closed Logger, but further implementation can implement kind pof Caching/Polling strategies
39   *
40   * @author christoph
41   */
42  class DataLoggerRespository {
43  
44      private static final String GLOBAL_FORMAT_PATTERN = "({0}_[a-z,A-Z,0-9]*[^_])_([0-9]*[^\\.])\\.[\\w]*";
45  
46      private static final String LOGGER_FORMAT_PATTERN = "({0})_([0-9]*[^\\.])\\.[\\w]*";
47  
48      private IDataLoggerFactory loggerFactory = null;
49  
50      private Map<String, IDataLogger> openLoggers = new HashMap<String, IDataLogger>();
51  
52      private long idGenerator = System.currentTimeMillis();
53  
54      /**
55       * ILoggereListeners watching the lifecycle of this logger
56       */
57      private List listeners = new ArrayList();
58  
59      private static final IPhynixxLogger LOGGER = PhynixxLogManager.getLogger(DataLoggerRespository.class);
60  
61      private String loggerSystemName = null;
62  
63      public DataLoggerRespository(String loggerSystemName, IDataLoggerFactory loggerFactory) {
64          super();
65          this.loggerSystemName = loggerSystemName;
66          this.loggerFactory = loggerFactory;
67      }
68  
69      public String getLoggerSystemName() {
70          return this.loggerSystemName;
71      }
72  
73  
74      public IDataLogger instanciateLogger(String loggerName, boolean open) throws IOException, InterruptedException {
75  
76          // is logger reopen
77          if (this.openLoggers.containsKey(loggerName)) {
78              return openLoggers.get(loggerName);
79          }
80  
81          IDataLogger logger = this.loggerFactory.instanciateLogger(loggerName);
82  
83          if (open) {
84              logger.reopen(AccessMode.WRITE);
85          }
86          this.openLoggers.put(loggerName, logger);
87  
88          return logger;
89      }
90  
91  
92      /**
93       * cloes a logger. This logger can be re-reopen
94       *
95       * @param loggerName
96       */
97      public void closeLogger(String loggerName) {
98          if (this.openLoggers.containsKey(loggerName)) {
99              try {
100                 this.openLoggers.get(loggerName).close();
101             } catch (Exception e) {
102                 throw new DelegatedRuntimeException(e);
103             }
104             this.openLoggers.remove(loggerName);
105         }
106     }
107 
108     /**
109      * destroys the logge an all its resources. the logger cannot be reopen and all logged information are vanished
110      *
111      * @param loggerName
112      */
113     public void destroyLogger(String loggerName) {
114     }
115 
116     public synchronized void destroy(String loggerName) {
117         if (this.openLoggers.containsKey(loggerName)) {
118             try {
119                 IDataLogger dataLogger = this.openLoggers.get(loggerName);
120                 dataLogger.close();
121             } catch (Exception e) {
122                 throw new DelegatedRuntimeException(e);
123             }
124             this.openLoggers.remove(loggerName);
125         }
126         this.loggerFactory.destroyLogger(loggerName);
127     }
128 
129 
130     /**
131      * recovers all {@link org.csc.phynixx.loggersystem.logger.IDataLogger} having LOGGER files
132      *
133      * @return
134      * @throws IOException
135      * @throws InterruptedException
136      */
137     public synchronized Set<IDataLogger> recover() throws Exception {
138         Set<IDataLogger> dataLoggers = new HashSet<IDataLogger>();
139 
140         Set<String> loggerNames = this.loggerFactory.findLoggerNames();
141         for (String loggerName : loggerNames) {
142             this.instanciateLogger(loggerName, true);
143         }
144         return dataLoggers;
145     }
146 
147     /**
148      * closes all reopen loggers
149      */
150     public synchronized void close() {
151         Map<String, IDataLogger> copiedLoggers = new HashMap(this.openLoggers);
152 
153         for (IDataLogger dataLogger : copiedLoggers.values()) {
154             try {
155                 dataLogger.close();
156             } catch (Exception e) {
157             }
158         }
159         this.openLoggers.clear();
160 
161     }
162 
163 }