View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.mylutece.modules.saml.authentication.config;
35  
36  import fr.paris.lutece.portal.service.util.AppException;
37  import fr.paris.lutece.portal.service.util.AppLogService;
38  import fr.paris.lutece.portal.service.util.AppPropertiesService;
39  
40  import java.io.FileNotFoundException;
41  import java.io.IOException;
42  
43  import java.util.Map;
44  import java.util.Properties;
45  
46  
47  public class ConfigProperties extends Properties
48  {
49      private static final long serialVersionUID = 6091203931131546429L;
50      private static ConfigProperties instance = null;
51      private boolean isTest = false;
52  
53      protected ConfigProperties(  )
54      {
55          try
56          {
57              init( "mylutece-saml-test.properties" );
58          }
59          catch ( FileNotFoundException ex )
60          {
61              String message = "File not found : " + ex.getMessage(  );
62              AppLogService.error( message );
63              throw new AppException( message, ex );
64          }
65          catch ( IOException ex )
66          {
67              String message = "Read error : " + ex.getMessage(  );
68              AppLogService.error( message );
69              throw new AppException( message, ex );
70          }
71      }
72  
73      public boolean isTest(  )
74      {
75          return instance.isTest;
76      }
77  
78      public void setTest( boolean isTest )
79      {
80          instance.isTest = isTest;
81      }
82  
83      public static void init(  ) throws Exception
84      {
85          if ( instance == null )
86          {
87              instance = new ConfigProperties(  );
88          }
89      }
90  
91      public static ConfigProperties getInstance(  )
92      {
93          if ( instance == null )
94          {
95              instance = new ConfigProperties(  );
96          }
97  
98          return instance;
99      }
100 
101     private void init( String name ) throws IOException
102     {
103         load( getClass(  ).getResourceAsStream( name ) );
104         expandVariables(  );
105     }
106 
107     @Override
108     public String getProperty( String key, String defaultValue )
109     {
110         if ( isTest )
111         {
112             return super.getProperty( key, defaultValue );
113         }
114         else
115         {
116             return AppPropertiesService.getProperty( key, defaultValue );
117         }
118     }
119 
120     @Override
121     public String getProperty( String key )
122     {
123         if ( isTest )
124         {
125             return super.getProperty( key );
126         }
127         else
128         {
129             return AppPropertiesService.getProperty( key );
130         }
131     }
132 
133     /**
134      * Expands ${var} using System properties.
135      */
136     private void expandVariables(  )
137     {
138         for ( Map.Entry<Object, Object> e : this.entrySet(  ) )
139         {
140             String value = (String) e.getValue(  );
141 
142             boolean replaced;
143 
144             do
145             {
146                 replaced = false;
147 
148                 // Replace ${<varkey>} with <varval>
149                 int begin = 0;
150 
151                 // Replace ${<varkey>} with <varval>
152                 int end = 0;
153 
154                 if ( ( begin = value.indexOf( "${" ) ) != -1 )
155                 {
156                     end = value.indexOf( '}', begin );
157 
158                     String varkey = value.substring( begin + 2, end );
159                     String varval = System.getProperty( varkey );
160 
161                     if ( varval != null )
162                     {
163                         value = value.substring( 0, begin ) + varval + value.substring( end + 1 );
164                         replaced = true;
165                     }
166                 }
167             }
168             while ( replaced );
169 
170             if ( !value.equals( (String) e.getValue(  ) ) )
171             {
172                 e.setValue( value );
173             }
174         }
175     }
176 }