Skip to content

Package: PropertiesStore$StorableElement

PropertiesStore$StorableElement

Coverage

1: /********************************************************************************
2: * Copyright (c) 2011 Eike Stepper (Berlin, Germany) and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Eike Stepper - initial API and implementation
13: ********************************************************************************/
14: package org.eclipse.emf.ecp.internal.core.util;
15:
16: import java.io.File;
17: import java.io.FileInputStream;
18: import java.io.FileOutputStream;
19: import java.io.IOException;
20: import java.io.InputStream;
21: import java.io.ObjectInput;
22: import java.io.ObjectInputStream;
23: import java.io.ObjectOutput;
24: import java.io.ObjectOutputStream;
25: import java.io.OutputStream;
26: import java.util.Collection;
27: import java.util.HashSet;
28: import java.util.Set;
29:
30: import org.eclipse.emf.ecp.core.util.ECPElement;
31: import org.eclipse.emf.ecp.core.util.observer.ECPObserver;
32: import org.eclipse.emf.ecp.internal.core.Activator;
33: import org.eclipse.emf.ecp.internal.core.util.PropertiesStore.StorableElement;
34: import org.eclipse.net4j.util.io.IOUtil;
35:
36: /**
37: * @author Eike Stepper
38: * @param <ELEMENT>
39: * @param <OBSERVER>
40: */
41: public abstract class PropertiesStore<ELEMENT extends StorableElement, OBSERVER extends ECPObserver> extends
42:         ElementRegistry<ELEMENT, OBSERVER> {
43:         private File folder;
44:
45:         /**
46:          * Default constructor.
47:          */
48:         public PropertiesStore() {
49:         }
50:
51:         /**
52:          *
53:          * @return The folder, where this property store stores its properties as a {@link File}
54:          */
55:         public final File getFolder() {
56:                 return folder;
57:         }
58:
59:         public final void setFolder(File folder) {
60:                 checkInactive();
61:                 this.folder = folder;
62:         }
63:
64:         @Override
65:         protected void doActivate() throws Exception {
66:                 super.doActivate();
67:                 if (folder == null) {
68:                         throw new IllegalStateException("Folder is null"); //$NON-NLS-1$
69:                 }
70:
71:                 if (folder.exists()) {
72:                         if (!folder.isDirectory()) {
73:                                 throw new IllegalStateException("Not a folder: " + folder); //$NON-NLS-1$
74:                         }
75:                 } else {
76:                         folder.mkdirs();
77:                 }
78:
79:                 load();
80:         }
81:
82:         protected void load() {
83:                 // TODO Trace properly
84:                 System.out.println("Loading " + getClass().getSimpleName() + " from " + folder.getAbsolutePath()); //$NON-NLS-1$ //$NON-NLS-2$
85:
86:                 final Set<ELEMENT> elements = new HashSet<ELEMENT>();
87:                 for (final File file : folder.listFiles()) {
88:                         try {
89:                                 if (isLoadableElement(file)) {
90:                                         InputStream stream = null;
91:
92:                                         try {
93:                                                 stream = new FileInputStream(file);
94:                                                 final ObjectInputStream in = new ObjectInputStream(stream);
95:                                                 final ELEMENT element = loadElement(in);
96:                                                 // ELEMENT existingElement = getElement(element.getName());
97:                                                 // if (existingElement != null)
98:                                                 // {
99:                                                 // if (element instanceof ECPDisposable)
100:                                                 // {
101:                                                 // ECPDisposable disposable = (ECPDisposable)element;
102:                                                 // disposable.dispose();
103:                                                 // }
104:                                                 // }
105:                                                 // else
106:                                                 {
107:                                                         elements.add(element);
108:                                                 }
109:                                         } finally {
110:                                                 IOUtil.close(stream);
111:                                         }
112:                                 }
113:                         } catch (final IOException ex) {
114:                                 Activator.log(ex);
115:                         }
116:                 }
117:
118:                 doChangeElements(null, elements);
119:         }
120:
121:         protected boolean isLoadableElement(File file) {
122:                 return file.isFile();
123:         }
124:
125:         /**
126:          * Loads an element.
127:          *
128:          * @param in an {@link ObjectInput} to load the element from
129:          * @return the element
130:          * @throws IOException if the element cannot be loaded correctly
131:          */
132:         protected abstract ELEMENT loadElement(ObjectInput in) throws IOException;
133:
134:         @Override
135:         protected void elementsChanged(Collection<ELEMENT> oldElements, Collection<ELEMENT> newElements) {
136:                 if (isDisposingElement()) {
137:                         return;
138:                 }
139:
140:                 if (isActive()) {
141:                         for (final ELEMENT element : InternalUtil.getRemovedElements(oldElements, newElements)) {
142:                                 try {
143:                                         final File file = getFile(element);
144:                                         file.delete();
145:                                 } catch (final Exception ex) {
146:                                         Activator.log(ex);
147:                                 }
148:                         }
149:
150:                         for (final ELEMENT element : InternalUtil.getAddedElements(oldElements, newElements)) {
151:                                 if (element.isStorable()) {
152:                                         storeElement(element);
153:                                 }
154:                         }
155:                 }
156:         }
157:
158:         public void storeElement(ELEMENT element) {
159:                 final File file = getFile(element);
160:                 final File temp = new File(file.getParentFile(), file.getName() + ".tmp"); //$NON-NLS-1$
161:                 if (temp.isFile()) {
162:                         temp.delete();
163:                 }
164:
165:                 OutputStream stream = null;
166:
167:                 try {
168:                         try {
169:                                 stream = new FileOutputStream(temp);
170:                                 final ObjectOutputStream out = new ObjectOutputStream(stream);
171:                                 element.write(out);
172:                                 out.flush();
173:                         } finally {
174:                                 IOUtil.close(stream);
175:                         }
176:
177:                         file.delete();
178:                         temp.renameTo(file);
179:                 } catch (final Exception ex) {
180:                         temp.delete();
181:                         Activator.log(ex);
182:                 }
183:         }
184:
185:         protected File getFile(ELEMENT element) {
186:                 return new File(folder, element.getName());
187:         }
188:
189:         /**
190:          * @author Eike Stepper
191:          */
192:         public interface StorableElement extends ECPElement {
193:                 boolean isStorable();
194:
195:                 void write(ObjectOutput out) throws IOException;
196:         }
197: }