1313
1414package org .codehaus .plexus .components .secdispatcher .internal ;
1515
16- import javax .inject .Inject ;
17- import javax .inject .Named ;
18- import javax .inject .Singleton ;
19-
2016import java .io .IOException ;
17+ import java .nio .file .Files ;
2118import java .nio .file .Path ;
22- import java .nio .file .Paths ;
2319import java .util .Collection ;
2420import java .util .HashMap ;
2521import java .util .List ;
4036import static java .util .Objects .requireNonNull ;
4137
4238/**
39+ * Note: this implementation is NOT a JSR330 component. Integrating apps anyway want to customize it (at least
40+ * the name and location of configuration file), so instead as before (providing "bad" configuration file just
41+ * to have one), it is the duty of integrator to wrap and "finish" the implementation in a way it suits the
42+ * integrator. Also, using "globals" like Java System Properties are bad thing, and it is integrator who knows
43+ * what is needed anyway.
44+ * <p>
45+ * Recommended way for integration is to create JSR330 {@link javax.inject.Provider}.
46+ *
4347 * @author Oleg Gusakov
4448 */
45- @ Singleton
46- @ Named
4749public class DefaultSecDispatcher implements SecDispatcher {
4850 public static final String ATTR_START = "[" ;
4951 public static final String ATTR_STOP = "]" ;
5052
5153 protected final PlexusCipher cipher ;
5254 protected final Map <String , Dispatcher > dispatchers ;
53- protected final String configurationFile ;
55+ protected final Path configurationFile ;
5456
55- @ Inject
56- public DefaultSecDispatcher (
57- PlexusCipher cipher ,
58- Map <String , Dispatcher > dispatchers ,
59- @ Named ("${configurationFile:-" + DEFAULT_CONFIGURATION + "}" ) final String configurationFile ) {
57+ public DefaultSecDispatcher (PlexusCipher cipher , Map <String , Dispatcher > dispatchers , Path configurationFile ) {
6058 this .cipher = requireNonNull (cipher );
6159 this .dispatchers = requireNonNull (dispatchers );
6260 this .configurationFile = requireNonNull (configurationFile );
61+
62+ // file may or may not exist, but one thing is certain: it cannot be an exiting directory
63+ if (Files .isDirectory (configurationFile )) {
64+ throw new IllegalArgumentException ("configurationFile cannot be a directory" );
65+ }
6366 }
6467
6568 @ Override
@@ -94,7 +97,7 @@ public Collection<Field> fields() {
9497 }
9598
9699 @ Override
97- public String encrypt (String str , Map <String , String > attr ) throws SecDispatcherException {
100+ public String encrypt (String str , Map <String , String > attr ) throws SecDispatcherException , IOException {
98101 if (isEncryptedString (str )) return str ;
99102
100103 try {
@@ -130,7 +133,7 @@ public String encrypt(String str, Map<String, String> attr) throws SecDispatcher
130133 }
131134
132135 @ Override
133- public String decrypt (String str ) throws SecDispatcherException {
136+ public String decrypt (String str ) throws SecDispatcherException , IOException {
134137 if (!isEncryptedString (str )) return str ;
135138 try {
136139 String bare = cipher .unDecorate (str );
@@ -149,7 +152,7 @@ public String decrypt(String str) throws SecDispatcherException {
149152
150153 @ Override
151154 public SettingsSecurity readConfiguration (boolean createIfMissing ) throws IOException {
152- SettingsSecurity configuration = SecUtil .read (getConfigurationPath () );
155+ SettingsSecurity configuration = SecUtil .read (configurationFile );
153156 if (configuration == null && createIfMissing ) {
154157 configuration = new SettingsSecurity ();
155158 }
@@ -159,10 +162,10 @@ public SettingsSecurity readConfiguration(boolean createIfMissing) throws IOExce
159162 @ Override
160163 public void writeConfiguration (SettingsSecurity configuration ) throws IOException {
161164 requireNonNull (configuration , "configuration is null" );
162- SecUtil .write (getConfigurationPath () , configuration , true );
165+ SecUtil .write (configurationFile , configuration , true );
163166 }
164167
165- private Map <String , String > prepareDispatcherConfig (String type ) {
168+ protected Map <String , String > prepareDispatcherConfig (String type ) throws IOException {
166169 HashMap <String , String > dispatcherConf = new HashMap <>();
167170 Map <String , String > conf = SecUtil .getConfig (getConfiguration (), type );
168171 if (conf != null ) {
@@ -171,7 +174,7 @@ private Map<String, String> prepareDispatcherConfig(String type) {
171174 return dispatcherConf ;
172175 }
173176
174- private String strip (String str ) {
177+ protected String strip (String str ) {
175178 int start = str .indexOf (ATTR_START );
176179 int stop = str .indexOf (ATTR_STOP );
177180 if (start != -1 && stop != -1 && stop > start ) {
@@ -180,7 +183,7 @@ private String strip(String str) {
180183 return str ;
181184 }
182185
183- private Map <String , String > stripAttributes (String str ) {
186+ protected Map <String , String > stripAttributes (String str ) {
184187 HashMap <String , String > result = new HashMap <>();
185188 int start = str .indexOf (ATTR_START );
186189 int stop = str .indexOf (ATTR_STOP );
@@ -202,30 +205,12 @@ private Map<String, String> stripAttributes(String str) {
202205 return result ;
203206 }
204207
205- private boolean isEncryptedString (String str ) {
208+ protected boolean isEncryptedString (String str ) {
206209 if (str == null ) return false ;
207210 return cipher .isEncryptedString (str );
208211 }
209212
210- private Path getConfigurationPath () {
211- String location = System .getProperty (SYSTEM_PROPERTY_CONFIGURATION_LOCATION , getConfigurationFile ());
212- location = location .charAt (0 ) == '~' ? System .getProperty ("user.home" ) + location .substring (1 ) : location ;
213- return Paths .get (location );
214- }
215-
216- private SettingsSecurity getConfiguration () throws SecDispatcherException {
217- Path path = getConfigurationPath ();
218- try {
219- SettingsSecurity sec = SecUtil .read (path );
220- if (sec == null )
221- throw new SecDispatcherException ("Please check that configuration file on path " + path + " exists" );
222- return sec ;
223- } catch (IOException e ) {
224- throw new SecDispatcherException (e .getMessage (), e );
225- }
226- }
227-
228- public String getConfigurationFile () {
229- return configurationFile ;
213+ protected SettingsSecurity getConfiguration () throws SecDispatcherException , IOException {
214+ return SecUtil .read (configurationFile );
230215 }
231216}
0 commit comments