protected void addSelection() { String key = keyText.getText(); String val = valueText.getText(); if (key != null && key.trim().length() > 0) { boolean isContained = false; for (int i = 0; i < globalLinkedProperties.size(); i++) { GlobalProperty property = (GlobalProperty) globalLinkedProperties.get(i); if (key.equals(property.key) && !property.isDeleted) { property.value = val; isContained = true; break; } } if (!isContained) { // if the file is read-only then change is not allowed. if (propFileName[0] == null) { return; } else { File f = new File(propFileName[0]); if (!(f.exists() && f.isFile())) { MessageDialog.openError( getShell(), Messages.getString("ResourceEditDialog.NotFile.Title"), // $NON-NLS-1$ Messages.getFormattedString( "ResourceEditDialog.NotFile.Message", //$NON-NLS-1$ new Object[] {propFileName})); return; } else if (!f.canWrite()) { MessageDialog.openError( getShell(), Messages.getString("ResourceEditDialog.ReadOnlyEncounter.Title"), // $NON-NLS-1$ Messages.getFormattedString( "ResourceEditDialog.ReadOnlyEncounter.Message", //$NON-NLS-1$ new Object[] {propFileName})); return; } GlobalProperty property = new GlobalProperty(); property.key = key; property.value = val; property.holder = contents[0]; property.isDeleted = false; property.holderFile = propFileName[0]; globalLinkedProperties.add(property); } } viewer.refresh(); listChanged = true; updateSelection(); } else { MessageDialog.openWarning( getShell(), Messages.getString("ResourceEditDialog.text.AddWarningTitle"), // $NON-NLS-1$ Messages.getString("ResourceEditDialog.text.AddWarningMsg")); // $NON-NLS-1$ } }
private void deleteSelection() { if (viewer.getTable().getSelectionIndex() == -1) { return; } StructuredSelection selection = (StructuredSelection) viewer.getSelection(); if (selection.getFirstElement() instanceof GlobalProperty) { GlobalProperty property = (GlobalProperty) selection.getFirstElement(); String file = property.holderFile; if (file != null) { File f = new File(file); if (!(f.exists() && f.isFile())) { MessageDialog.openError( getShell(), Messages.getString("ResourceEditDialog.NotFile.Title"), // $NON-NLS-1$ Messages.getFormattedString( "ResourceEditDialog.NotFile.Message", //$NON-NLS-1$ new Object[] {file})); return; } else if (!f.canWrite()) { MessageDialog.openError( getShell(), Messages.getString("ResourceEditDialog.ReadOnlyEncounter.Title"), // $NON-NLS-1$ Messages.getFormattedString( "ResourceEditDialog.ReadOnlyEncounter.Message", //$NON-NLS-1$ new Object[] {file})); return; } } // if the file is read-only then change is not allowed. listChanged = true; if (property.holderFile != null) property.isDeleted = true; else globalLinkedProperties.remove(property); viewer.refresh(); updateSelection(); } }
/** Loads the key/value from message file. */ private void loadMessage() { if (this.resourceURLs != null && this.resourceURLs.length > 0) { if (contents == null) contents = new LinkedProperties[resourceURLs.length]; if (propFileName == null) propFileName = new String[resourceURLs.length]; LinkedHashMap<String, GlobalProperty> propertyMap = new LinkedHashMap<String, GlobalProperty>(); for (int i = 0; i < resourceURLs.length; i++) { contents[i] = new LinkedProperties(); try { if (this.resourceURLs[i] != null) { InputStream in = this.resourceURLs[i].openStream(); contents[i].load(in); in.close(); propFileName[i] = DEUtil.getFilePathFormURL(resourceURLs[i]); Iterator iter = contents[i].keySet().iterator(); if (iter != null) { while (iter.hasNext()) { String key = (String) iter.next(); if (!propertyMap.containsKey(key)) { GlobalProperty property = new GlobalProperty(); property.key = key; property.value = contents[i].getProperty(key); property.holder = contents[i]; property.isDeleted = false; property.holderFile = propFileName[i]; propertyMap.put(key, property); } } } } } catch (Exception e) { ExceptionHandler.handle(e); } } for (Iterator iter = propertyMap.keySet().iterator(); iter.hasNext(); ) { globalLinkedProperties.add(propertyMap.get(iter.next())); } } }
private static void linkGlobalProperty(Class clz, Map<String, String> propertiesMap) { for (Field f : clz.getDeclaredFields()) { GlobalProperty at = f.getAnnotation(GlobalProperty.class); if (at == null) { continue; } if (!Modifier.isStatic(f.getModifiers())) { throw new CloudRuntimeException(String.format("%s.%s is annotated by @GlobalProperty but it's not defined with static modifier", clz.getName(), f.getName())); } Object valueToSet = null; String name = at.name(); if (Map.class.isAssignableFrom(f.getType())) { Map ret = linkGlobalPropertyMap(name); if (ret.isEmpty() && at.required()) { throw new IllegalArgumentException(String.format("A required global property[%s] missing in zstack.properties", name)); } valueToSet = ret; } else if (List.class.isAssignableFrom(f.getType())) { List ret = linkGlobalPropertyList(name); if (ret.isEmpty() && at.required()) { throw new IllegalArgumentException(String.format("A required global property[%s] missing in zstack.properties", name)); } valueToSet = ret; } else { String value = getGlobalProperty(name); if (value == null && at.defaultValue().equals(GlobalProperty.DEFAULT_NULL_STRING) && at.required()) { throw new IllegalArgumentException(String.format("A required global property[%s] missing in zstack.properties", name)); } if (value == null) { value = at.defaultValue(); } if (GlobalProperty.DEFAULT_NULL_STRING.equals(value)) { value = null; } if (value != null) { value = StringTemplate.subsititute(value, propertiesMap); } if (Integer.class.isAssignableFrom(f.getType()) || Integer.TYPE.isAssignableFrom(f.getType())) { valueToSet = TypeUtils.stringToValue(value, Integer.class, 0); } else if (Long.class.isAssignableFrom(f.getType()) || Long.TYPE.isAssignableFrom(f.getType())) { valueToSet = TypeUtils.stringToValue(value, Long.class, 0L); } else if (Float.class.isAssignableFrom(f.getType()) || Float.TYPE.isAssignableFrom(f.getType())) { valueToSet = TypeUtils.stringToValue(value, Float.class, 0F); } else if (Double.class.isAssignableFrom(f.getType()) || Double.TYPE.isAssignableFrom(f.getType())) { valueToSet = TypeUtils.stringToValue(value, Double.class, 0D); } else if (String.class.isAssignableFrom(f.getType())) { valueToSet = value; } else if (Boolean.class.isAssignableFrom(f.getType()) || Boolean.TYPE.isAssignableFrom(f.getType())) { valueToSet = TypeUtils.stringToValue(value, Boolean.class); } else { throw new CloudRuntimeException(String.format("%s.%s of type[%s] is unsupported by global property. try use Platform.getGlobalProperty() and parse by yourself", clz.getName(), f.getName(), f.getType().getName())); } } f.setAccessible(true); try { f.set(null, valueToSet); globalProperties.put(name, valueToSet == null ? "null" : valueToSet.toString()); logger.debug(String.format("linked global property[%s.%s], value: %s", clz.getName(), f.getName(), valueToSet)); } catch (IllegalAccessException e) { throw new CloudRuntimeException(String.format("unable to link global property[%s.%s]", clz.getName(), f.getName()), e); } } }