*/
public abstract Queue<VMProperty> getProperties();
- /**
- * Create 'VMProperty' that have empty emulator configuration
- * @param name
- * @return
- */
- public synchronized VMProperty newProperty(VMPropertyValue value) {
- return new VMProperty(value);
+ public VMProperty newProperty(VMPropertyValue value) {
+ return VMProperty.createNew(value);
}
public VMProperty newProperty(Path propertyFile) {
- VMProperty property = new VMProperty(propertyFile);
-
- return property;
+ return VMProperty.loadFrom(propertyFile);
}
- EmulatorConfiguration parseXML(Path propertyFile) {
+ synchronized EmulatorConfiguration parseXML(Path propertyFile) {
JAXBElement<EmulatorConfiguration> element = null;
- synchronized (context) {
+ FileChannel fc = null;
+ FileLock lock = null;
+ try {
+ fc = FileChannel.open(propertyFile, StandardOpenOption.READ, StandardOpenOption.WRITE);
+ lock = fc.lock();
+
+ Unmarshaller unmarshaller = context.createUnmarshaller();
+ element = unmarshaller.unmarshal(new StreamSource(Channels.newInputStream(fc)),
+ EmulatorConfiguration.class);
+ fc.close();
+ } catch (OverlappingFileLockException e) {
+ // have not to enter here
+ e.printStackTrace();
+ assert false;
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (JAXBException e) {
+ e.printStackTrace();
+ EMLogger.getLogger().warning("Can not load config file( " //$NON-NLS-1$
+ + propertyFile + ")" + StringResources.NEW_LINE + e.getMessage()); //$NON-NLS-1$
+ } finally {
try {
- FileChannel fc = FileChannel.open(propertyFile, StandardOpenOption.READ, StandardOpenOption.WRITE);
- FileLock lock = fc.lock();
-
-
- try {
- Unmarshaller unmarshaller = context.createUnmarshaller();
- element = unmarshaller.unmarshal(new StreamSource(Channels.newInputStream(fc)),
- EmulatorConfiguration.class);
- fc.close();
- } catch (IOException e) {
- e.printStackTrace();
+ if (lock != null && lock.isValid()) {
+ lock.release();
}
- if (lock.isValid()) {
- lock.release();
+ if (fc != null && fc.isOpen()) {
+ fc.close();
}
- fc.close();
- } catch (OverlappingFileLockException e) {
- // have not to enter here
- e.printStackTrace();
- assert false;
} catch (IOException e) {
e.printStackTrace();
- } catch (JAXBException e) {
- e.printStackTrace();
- EMLogger.getLogger().warning("Can not load config file( " //$NON-NLS-1$
- + propertyFile + ")" + StringResources.NEW_LINE + e.getMessage()); //$NON-NLS-1$
}
}
return null;
}
- boolean storeXML(VMProperty property){
+ synchronized boolean storeXML(VMProperty property){
Path outputFile = property.getPropertyFile();
- synchronized (context) {
- FileLock lock = null;
- FileChannel fc = null;
- try {
- Files.createDirectories(property.getPropertyFile().resolve("..").normalize());
+ FileChannel fc = null;
+ FileLock lock = null;
+ try {
+ Files.createDirectories(property.getPropertyFile().resolve("..").normalize());
- fc = FileChannel.open(outputFile, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
- lock = fc.lock();
+ fc = FileChannel.open(outputFile, StandardOpenOption.CREATE, StandardOpenOption.WRITE,
+ StandardOpenOption.TRUNCATE_EXISTING);
+ lock = fc.lock();
- assert lock != null;
+ assert lock != null;
- if (!lock.isValid()) {
- return false;
- }
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
-
- OutputStream os = null;
- try {
- Marshaller marshaller = context.createMarshaller();
- marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); //$NON-NLS-1$
- marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
-
- os = Channels.newOutputStream(fc);
- marshaller.marshal(property.getVMConfiguration().getConfiguration(), os);
- } catch (JAXBException e) {
- e.printStackTrace();
+ if (!lock.isValid()) {
return false;
}
+ } catch (IOException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
- try {
+ OutputStream os = null;
+ try {
+ Marshaller marshaller = context.createMarshaller();
+ marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); //$NON-NLS-1$
+ marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
+
+ os = Channels.newOutputStream(fc);
+ marshaller.marshal(property.getVMConfiguration().getConfiguration(), os);
+ } catch (JAXBException e) {
+ e.printStackTrace();
+ return false;
+ }
+
+ try {
+ if (lock != null && lock.isValid()) {
lock.release();
+ }
+ if (fc != null && fc.isOpen()) {
fc.close();
- } catch (IOException e) {
- e.printStackTrace();
}
+ } catch (IOException e) {
+ e.printStackTrace();
}
return true;
NOT_CHECKED,
WELL,
NOT_AVAILABLE,
+ CORRUPTED,
}
public static enum State {
stateChangelisteners.remove(listener);
}
- private void stateChanged() {
- synchronized (stateChangelisteners) {
- for (StateChangeListener<VMProperty> listener : stateChangelisteners) {
- listener.stateChanged(this);
- }
- }
- }
-
public static void addHealthChangeListener(StateChangeListener<VMProperty> listener) {
healthChangelisteners.add(listener);
}
healthChangelisteners.remove(listener);
}
- private void healthChanged() {
- synchronized (healthChangelisteners) {
- for (StateChangeListener<VMProperty> listener : healthChangelisteners) {
- listener.stateChanged(this);
- }
- }
- }
+ static VMProperty loadFrom(Path propertyFile) {
+ VMProperty property = new VMProperty(propertyFile);
+ property.refresh();
- @Override
- public boolean equals(Object obj) {
- if (obj == null) {
- return false;
- }
-
- VMProperty target = (VMProperty)obj;
- if (target.getName().equals(getName())) {
- return true;
- }
- return false;
- }
-
- VMProperty(Path propertyFile) {
- this.propertyFile = propertyFile;
-
- refresh();
+ return property;
}
- VMProperty(VMPropertyValue value) {
+ static VMProperty createNew(VMPropertyValue value) {
assert value != null;
String directoryName = FilePathResources.getTizenVmsPath() + File.separator + value.vmsName;
String propertyFilename = directoryName + File.separator + StringResources.PROPERTY_XML_NAME;
Path propertyFile = Paths.get(propertyFilename);
+ assert Files.notExists(propertyFile);
+ VMProperty property = new VMProperty(propertyFile);
+ property.setPropertyValue(value);
+
+ return property;
+ }
+
+ private VMProperty(Path propertyFile) {
this.propertyFile = propertyFile;
- assert Files.notExists(propertyFile);
+ }
+
+ private void stateChanged() {
+ synchronized (stateChangelisteners) {
+ for (StateChangeListener<VMProperty> listener : stateChangelisteners) {
+ listener.stateChanged(this);
+ }
+ }
+ }
- setPropertyValue(value);
+ private void healthChanged() {
+ synchronized (healthChangelisteners) {
+ for (StateChangeListener<VMProperty> listener : healthChangelisteners) {
+ listener.stateChanged(this);
+ }
+ }
}
void refresh() {
EmulatorConfiguration element = EmulatorManager.getVMKeeper().parseXML(propertyFile);
+ // If vm_config.xml is corrupted
+ if (element == null) {
+ setHealth(Health.CORRUPTED);
+ }
+
this.configuration = new VMPropertyConfiguration(element);
if (configuration.getName() != null) {
return this.health == Health.WELL && this.state != State.NOT_SET;
}
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == null) {
+ return false;
+ }
+
+ VMProperty target = (VMProperty)obj;
+ if (target.getName().equals(getName())) {
+ return true;
+ }
+ return false;
+ }
+
// for plug-in source -> need to delete
@ Deprecated
public EmulatorConfiguration getConfiguration() {