View Javadoc

1   /*
2    * Copyright 2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.admin.domain.model;
17  
18  import static de.smartics.properties.admin.domain.service.ApplicationIdConfigurationLoader.ARCHIVE_TYPE;
19  import static de.smartics.properties.admin.domain.service.ApplicationIdConfigurationLoader.ARTIFACT_ID;
20  import static de.smartics.properties.admin.domain.service.ApplicationIdConfigurationLoader.CLASSIFIER;
21  import static de.smartics.properties.admin.domain.service.ApplicationIdConfigurationLoader.GROUP_ID;
22  import static de.smartics.properties.admin.domain.service.ApplicationIdConfigurationLoader.VERSION;
23  import static de.smartics.properties.api.core.security.SecurityConfigurationLoader.SECURITY_ALGORITHM;
24  import static de.smartics.properties.api.core.security.SecurityConfigurationLoader.SECURITY_KEY;
25  import static de.smartics.properties.api.core.security.SecurityConfigurationLoader.SECURITY_PROVIDER;
26  import static de.smartics.properties.api.core.security.SecurityConfigurationLoader.SECURITY_TRANSFORMATION;
27  import static de.smartics.properties.impl.config.ds.DataSourceConfiguration.CREATE_TABLE;
28  import static de.smartics.properties.impl.config.ds.DataSourceConfiguration.DROP_TABLE;
29  import static de.smartics.properties.impl.config.ds.DataSourceConfiguration.JNDI_NAME;
30  
31  import java.io.Serializable;
32  
33  import javax.naming.Context;
34  import javax.naming.InitialContext;
35  import javax.naming.NamingException;
36  
37  import de.smartics.properties.api.core.app.JndiContext;
38  import de.smartics.properties.spi.config.cache.CacheConfigurationLoader;
39  
40  /**
41   * Stores JNDI configuration properties.
42   */
43  public final class JndiConfiguration implements Serializable
44  {
45    // ********************************* Fields *********************************
46  
47    // --- constants ------------------------------------------------------------
48  
49    /**
50     * The class version identifier.
51     * <p>
52     * The value of this constant is {@value}.
53     * </p>
54     */
55    private static final long serialVersionUID = 1L;
56  
57    /**
58     * The provider of base configuration properties fetched from the JNDI
59     * context.
60     *
61     * @serial
62     */
63    private static final JndiConfiguration INSTANCE = new JndiConfiguration();
64  
65    // --- members --------------------------------------------------------------
66  
67    /**
68     * Access to the JNDI context.
69     */
70    private JndiContext lookup;
71  
72    /**
73     * The group identifier of the application the configuration is associated
74     * with.
75     *
76     * @serial
77     */
78    private String groupId;
79  
80    /**
81     * The artifact identifier of the application the configuration is associated
82     * with.
83     *
84     * @serial
85     */
86    private String artifactId;
87  
88    /**
89     * The version of the application the configuration is associated with.
90     *
91     * @serial
92     */
93    private String version;
94  
95    /**
96     * The archive type of the application the configuration is associated with.
97     *
98     * @serial
99     */
100   private String archiveType;
101 
102   /**
103    * The classifier of the application the configuration is associated with.
104    *
105    * @serial
106    */
107   private String classifier;
108 
109   /**
110    * The name of the cache within the JNDI to be looked up.
111    *
112    * @serial
113    */
114   private String cacheName;
115 
116   /**
117    * The name of the datasource within the JNDI to be looked up.
118    *
119    * @serial
120    */
121   private String dsName;
122 
123   /**
124    * The flag to the datasource to create the properties table.
125    *
126    * @serial
127    */
128   private Boolean dsCreateTable;
129 
130   /**
131    * The flag to the datasource to drop the properties table.
132    *
133    * @serial
134    */
135   private Boolean dsDropTable;
136 
137   /**
138    * The security key.
139    *
140    * @serial
141    */
142   private String securityKey;
143 
144   /**
145    * The security algorithm.
146    *
147    * @serial
148    */
149   private String securityAlgorithm;
150 
151   /**
152    * The security provider.
153    *
154    * @serial
155    */
156   private String securityProvider;
157 
158   /**
159    * The security transformation.
160    *
161    * @serial
162    */
163   private String securityTransformation;
164 
165   // ****************************** Initializer *******************************
166 
167   // ****************************** Constructors ******************************
168 
169   /**
170    * Default constructor.
171    */
172   public JndiConfiguration()
173   {
174   }
175 
176   // ****************************** Inner Classes *****************************
177 
178   // ********************************* Methods ********************************
179 
180   // --- init -----------------------------------------------------------------
181 
182   /**
183    * Initializes the configuration by copying the property values from the JNDI.
184    *
185    * @throws IllegalStateException on any problem accessing the JNDI context.
186    */
187   public void initialize() throws IllegalStateException
188   {
189     try
190     {
191       final Context jndi = new InitialContext();
192       final JndiContext context = JndiContext.boot(jndi);
193 
194       setGroupId(context.lookup(GROUP_ID));
195       setArtifactId(context.lookup(ARTIFACT_ID));
196       setVersion(context.lookup(VERSION));
197       setArchiveType(context.lookup(ARCHIVE_TYPE));
198       setClassifier(context.lookup(CLASSIFIER));
199 
200       setDsName(context.lookup(JNDI_NAME));
201       setDsCreateTable(context.lookupBoolean(CREATE_TABLE));
202       setDsDropTable(context.lookupBoolean(DROP_TABLE));
203 
204       setCacheName(context.lookup(CacheConfigurationLoader.JNDI_NAME));
205 
206       setSecurityKey(context.lookup(SECURITY_KEY));
207       setSecurityAlgorithm(context.lookup(SECURITY_ALGORITHM));
208       setSecurityProvider(context.lookup(SECURITY_PROVIDER));
209       setSecurityTransformation(context.lookup(SECURITY_TRANSFORMATION));
210 
211       this.lookup = context;
212     }
213     catch (final Exception e)
214     {
215       throw new IllegalStateException("Cannot initialize JNDI context.", e);
216     }
217   }
218 
219   /**
220    * Checks if the configuration has already been initialized.
221    *
222    * @return <code>true</code> if already initialized, <code>false</code>
223    *         otherwise.
224    */
225   public boolean isInitialized()
226   {
227     return lookup != null;
228   }
229 
230   // --- get&set --------------------------------------------------------------
231 
232   /**
233    * Returns the group identifier of the application the configuration is
234    * associated with.
235    *
236    * @return the group identifier of the application the configuration is
237    *         associated with.
238    */
239   public String getGroupId()
240   {
241     return groupId;
242   }
243 
244   /**
245    * Sets the group identifier of the application the configuration is
246    * associated with.
247    *
248    * @param groupId the group identifier of the application the configuration is
249    *          associated with.
250    */
251   public void setGroupId(final String groupId)
252   {
253     this.groupId = groupId;
254   }
255 
256   /**
257    * Returns the artifact identifier of the application the configuration is
258    * associated with.
259    *
260    * @return the artifact identifier of the application the configuration is
261    *         associated with.
262    */
263   public String getArtifactId()
264   {
265     return artifactId;
266   }
267 
268   /**
269    * Sets the artifact identifier of the application the configuration is
270    * associated with.
271    *
272    * @param artifactId the artifact identifier of the application the
273    *          configuration is associated with.
274    */
275   public void setArtifactId(final String artifactId)
276   {
277     this.artifactId = artifactId;
278   }
279 
280   /**
281    * Returns the version of the application the configuration is associated
282    * with.
283    *
284    * @return the version of the application the configuration is associated
285    *         with.
286    */
287   public String getVersion()
288   {
289     return version;
290   }
291 
292   /**
293    * Sets the version of the application the configuration is associated with.
294    *
295    * @param version the version of the application the configuration is
296    *          associated with.
297    */
298   public void setVersion(final String version)
299   {
300     this.version = version;
301   }
302 
303   /**
304    * Returns the archive type of the application the configuration is associated
305    * with.
306    *
307    * @return the archive type of the application the configuration is associated
308    *         with.
309    */
310   public String getArchiveType()
311   {
312     return archiveType;
313   }
314 
315   /**
316    * Sets the archive type of the application the configuration is associated
317    * with.
318    *
319    * @param archiveType the archive type of the application the configuration is
320    *          associated with.
321    */
322   public void setArchiveType(final String archiveType)
323   {
324     this.archiveType = archiveType;
325   }
326 
327   /**
328    * Returns the classifier of the application the configuration is associated
329    * with.
330    *
331    * @return the classifier of the application the configuration is associated
332    *         with.
333    */
334   public String getClassifier()
335   {
336     return classifier;
337   }
338 
339   /**
340    * Sets the classifier of the application the configuration is associated
341    * with.
342    *
343    * @param classifier the classifier of the application the configuration is
344    *          associated with.
345    */
346   public void setClassifier(final String classifier)
347   {
348     this.classifier = classifier;
349   }
350 
351   /**
352    * Returns the name of the cache within the JNDI to be looked up.
353    *
354    * @return the name of the cache within the JNDI to be looked up.
355    */
356   public String getCacheName()
357   {
358     return cacheName;
359   }
360 
361   /**
362    * Sets the name of the cache within the JNDI to be looked up.
363    *
364    * @param cacheName the name of the cache within the JNDI to be looked up.
365    */
366   public void setCacheName(final String cacheName)
367   {
368     this.cacheName = cacheName;
369   }
370 
371   /**
372    * Returns the name of the datasource within the JNDI to be looked up.
373    *
374    * @return the name of the datasource within the JNDI to be looked up.
375    */
376   public String getDsName()
377   {
378     return dsName;
379   }
380 
381   /**
382    * Sets the name of the datasource within the JNDI to be looked up.
383    *
384    * @param dsName the name of the datasource within the JNDI to be looked up.
385    */
386   public void setDsName(final String dsName)
387   {
388     this.dsName = dsName;
389   }
390 
391   /**
392    * Returns the flag to the datasource to create the properties table.
393    *
394    * @return the flag to the datasource to create the properties table.
395    */
396   public Boolean getDsCreateTable()
397   {
398     return dsCreateTable;
399   }
400 
401   /**
402    * Sets the flag to the datasource to create the properties table.
403    *
404    * @param dsCreateTable the flag to the datasource to create the properties
405    *          table.
406    */
407   public void setDsCreateTable(final Boolean dsCreateTable)
408   {
409     this.dsCreateTable = dsCreateTable;
410   }
411 
412   /**
413    * Sets the flag to the datasource to create the properties table.
414    *
415    * @param dsCreateTable the flag to the datasource to create the properties
416    *          table.
417    */
418   public void setDsCreateTable(final String dsCreateTable)
419   {
420     if (dsCreateTable != null)
421     {
422       setDsCreateTable("on".equals(dsCreateTable));
423     }
424     else
425     {
426       setDsCreateTable((Boolean) null);
427     }
428   }
429 
430   /**
431    * Returns the flag to the datasource to drop the properties table.
432    *
433    * @return the flag to the datasource to drop the properties table.
434    */
435   public Boolean getDsDropTable()
436   {
437     return dsDropTable;
438   }
439 
440   /**
441    * Sets the flag to the datasource to drop the properties table.
442    *
443    * @param dsDropTable the flag to the datasource to drop the properties table.
444    */
445   public void setDsDropTable(final Boolean dsDropTable)
446   {
447     this.dsDropTable = dsDropTable;
448   }
449 
450   /**
451    * Sets the flag to the datasource to drop the properties table.
452    *
453    * @param dsDropTable the flag to the datasource to drop the properties table.
454    */
455   public void setDsDropTable(final String dsDropTable)
456   {
457     if (dsDropTable != null)
458     {
459       setDsDropTable("on".equals(dsDropTable));
460     }
461     else
462     {
463       setDsDropTable((Boolean) null);
464     }
465   }
466 
467   /**
468    * Returns the security key.
469    *
470    * @return the security key.
471    */
472   public String getSecurityKey()
473   {
474     return securityKey;
475   }
476 
477   /**
478    * Sets the security key.
479    *
480    * @param securityKey the security key.
481    */
482   public void setSecurityKey(final String securityKey)
483   {
484     this.securityKey = securityKey;
485   }
486 
487   /**
488    * Returns the security algorithm.
489    *
490    * @return the security algorithm.
491    */
492   public String getSecurityAlgorithm()
493   {
494     return securityAlgorithm;
495   }
496 
497   /**
498    * Sets the security algorithm.
499    *
500    * @param securityAlgorithm the security algorithm.
501    */
502   public void setSecurityAlgorithm(final String securityAlgorithm)
503   {
504     this.securityAlgorithm = securityAlgorithm;
505   }
506 
507   /**
508    * Returns the security provider.
509    *
510    * @return the security provider.
511    */
512   public String getSecurityProvider()
513   {
514     return securityProvider;
515   }
516 
517   /**
518    * Sets the security provider.
519    *
520    * @param securityProvider the security provider.
521    */
522   public void setSecurityProvider(final String securityProvider)
523   {
524     this.securityProvider = securityProvider;
525   }
526 
527   /**
528    * Returns the security transformation.
529    *
530    * @return the security transformation.
531    */
532   public String getSecurityTransformation()
533   {
534     return securityTransformation;
535   }
536 
537   /**
538    * Sets the security transformation.
539    *
540    * @param securityTransformation the security transformation.
541    */
542   public void setSecurityTransformation(final String securityTransformation)
543   {
544     this.securityTransformation = securityTransformation;
545   }
546 
547   // --- business -------------------------------------------------------------
548 
549   /**
550    * Updates the JNDI context with the values in this configuration.
551    *
552    * @throws NamingException on any problem setting any value.
553    */
554   public void update() throws NamingException
555   {
556     lookup.set(GROUP_ID, getGroupId());
557     lookup.set(ARTIFACT_ID, getArtifactId());
558     lookup.set(VERSION, getVersion());
559     lookup.set(ARCHIVE_TYPE, getArchiveType());
560     lookup.set(CLASSIFIER, getClassifier());
561 
562     lookup.set(JNDI_NAME, getDsName());
563     lookup.set(CREATE_TABLE, getDsCreateTable());
564     lookup.set(DROP_TABLE, getDsDropTable());
565 
566     lookup.set(CacheConfigurationLoader.JNDI_NAME, getCacheName());
567 
568     lookup.set(SECURITY_KEY, getSecurityKey());
569     lookup.set(SECURITY_ALGORITHM, getSecurityAlgorithm());
570     lookup.set(SECURITY_PROVIDER, getSecurityProvider());
571     lookup.set(SECURITY_TRANSFORMATION, getSecurityTransformation());
572   }
573 
574   /**
575    * Provides access to the JNDI configuration.
576    *
577    * @return the JNDI configuration.
578    */
579   public static JndiConfiguration getJndiConfiguration()
580   {
581     return INSTANCE;
582   }
583 
584   // --- object basics --------------------------------------------------------
585 
586 }