Skip to main content
summaryrefslogtreecommitdiffstats
blob: d082c8954c3f532a0f90f64fd2fbe6ff1da02762 (plain) (blame)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Copyright (c) 2008-2012, 2016 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.net4j.util.security;

import org.eclipse.net4j.util.StringUtil;
import org.eclipse.net4j.util.io.IORuntimeException;
import org.eclipse.net4j.util.io.IOUtil;
import org.eclipse.net4j.util.om.OMPlatform;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

/**
 * @author Eike Stepper
 */
public class FileUserManager extends UserManager
{
  private static final boolean FALL_BACK_TO_CONFIG_FOLDER = OMPlatform.INSTANCE.isProperty("net4j.security.FileUserManager.fallBackToConfigFolder");

  private static final String CONFIG_FOLDER_PREFIX = "@config/";

  protected String fileName;

  private File file;

  public FileUserManager()
  {
  }

  public String getFileName()
  {
    return fileName;
  }

  /**
   * Sets the name of the file to be used by this user manager.
   * <p>
   * The {@link #getFile() file} is resolved in the following order:
   * <ol>
   * <li> If it starts with the path segment &quot;@config&quot; the subsequent path segments are interpreted as relative to the {@link OMPlatform#getConfigFolder() config folder}.
   * <li> If it is relative it is interpreted as relative to the application's current directory.
   * <li> Otherwise it is interpreted as absolute.
   * </ol>
   * The resolved file is not required to exist when this user manager is activated. In this case it will be created when {@link #addUser(String, char[]) addUser()}
   * or {@link #removeUser(String) removeUser()} are called.
   * <p>
   * With &quot;-Dnet4j.security.FileUserManager.fallBackToConfigFolder=true&quot; a relative path is resolved in both the application's current folder
   * and the config folder (in this order).
   */
  public void setFileName(String fileName)
  {
    checkInactive();
    this.fileName = fileName;
  }

  /**
   * @since 3.7
   */
  public final File getFile()
  {
    return file;
  }

  /**
   * @since 3.7
   */
  protected File resolveFile(String fileName) throws Exception
  {
    if (StringUtil.isEmpty(fileName))
    {
      return null;
    }

    if (fileName.replace('\\', '/').startsWith(CONFIG_FOLDER_PREFIX))
    {
      return OMPlatform.INSTANCE.getConfigFile(fileName.substring(CONFIG_FOLDER_PREFIX.length()));
    }

    File file = new File(fileName);

    if (FALL_BACK_TO_CONFIG_FOLDER && !file.isFile())
    {
      File configFile = OMPlatform.INSTANCE.getConfigFile(fileName);
      if (configFile != null && configFile.isFile())
      {
        return configFile;
      }
    }

    return file;
  }

  @Override
  protected void doBeforeActivate() throws Exception
  {
    super.doBeforeActivate();

    file = resolveFile(fileName);
    if (file != null)
    {
      file = file.getCanonicalFile();
    }
  }

  @Override
  protected void doDeactivate() throws Exception
  {
    super.doDeactivate();
    file = null;
  }

  @Override
  protected void load(Map<String, char[]> users) throws IORuntimeException
  {
    if (file != null && file.isFile())
    {
      FileInputStream stream = IOUtil.openInputStream(file);

      try
      {
        load(users, stream);
      }
      catch (IOException ex)
      {
        throw new IORuntimeException(ex);
      }
      finally
      {
        IOUtil.closeSilent(stream);
      }
    }
  }

  protected void load(Map<String, char[]> users, InputStream stream) throws IOException
  {
    Properties properties = new Properties();
    properties.load(stream);

    for (Entry<Object, Object> entry : properties.entrySet())
    {
      String userID = (String)entry.getKey();
      char[] password = ((String)entry.getValue()).toCharArray();
      users.put(userID, password);
    }
  }

  @Override
  protected void save(Map<String, char[]> users) throws IORuntimeException
  {
    if (file != null)
    {
      file.getParentFile().mkdirs();
      FileOutputStream stream = IOUtil.openOutputStream(file);

      try
      {
        save(users, stream);
      }
      catch (IOException ex)
      {
        throw new IORuntimeException(ex);
      }
      finally
      {
        IOUtil.closeSilent(stream);
      }
    }
  }

  protected void save(Map<String, char[]> users, FileOutputStream stream) throws IOException
  {
    Properties properties = new Properties();

    for (Entry<String, char[]> entry : users.entrySet())
    {
      properties.put(entry.getKey(), new String(entry.getValue()));
    }

    String comment = MessageFormat.format("User database {0,date} {0,time,HH:mm:ss:SSS}", System.currentTimeMillis());
    properties.store(stream, comment);
  }
}

Back to the top