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 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
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
149 int begin = 0;
150
151
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 }