BrightSide Workbench Full Report + Source Code
UserPreferences.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.impl.security;
19 
20 import java.io.File;
21 import java.util.HashMap;
22 import org.turro.elephant.context.ElephantContext;
23 import org.turro.reflection.Serializer;
24 
29 public class UserPreferences extends HashMap<String, Object> {
30 
31  private static final long serialVersionUID = 1L;
32 
33  private static final String PREFERENCES_ROOT = "/WEB-INF/elephant/preferences";
34  private transient String id;
35  private transient File file;
36 
37  public static UserPreferences getSystem() {
38  return getInstance("ElephantSystem");
39  }
40 
41  public static UserPreferences getInstance(String id) {
42  File root = new File(ElephantContext.getRealPath(PREFERENCES_ROOT));
43  if (!root.exists()) {
44  root.mkdirs();
45  }
46  File file = new File(ElephantContext.getRealPath(PREFERENCES_ROOT + "/" + id + ".preferences"));
47  UserPreferences up = (UserPreferences) Serializer.deserialize(file);
48  if(up == null) {
49  up = new UserPreferences();
50  }
51  up.setId(id);
52  up.setFile(file);
53  return up;
54  }
55 
56  public boolean isEqual(String id) {
57  return this.id.equals(id);
58  }
59 
60  @Override
61  public Object put(String key, Object value) {
62  Object obj = super.put(key, value);
63  Serializer.serialize(file, this);
64  return obj;
65  }
66 
67  private void setId(String id) {
68  this.id = id;
69  }
70 
71  private void setFile(File file) {
72  this.file = file;
73  }
74 
75 }
static UserPreferences getInstance(String id)