Search in sources :

Example 56 with HashSet

use of java.util.HashSet in project disconf by knightliao.

the class ReloadingPropertyPlaceholderConfigurer method propertiesReloaded.

/**
     * 当配置更新时,被调用
     *
     * @param event
     */
public void propertiesReloaded(PropertiesReloadedEvent event) {
    Properties oldProperties = lastMergedProperties;
    try {
        //
        Properties newProperties = mergeProperties();
        //
        // 获取哪些 dynamic property 被影响
        //
        Set<String> placeholders = placeholderToDynamics.keySet();
        Set<DynamicProperty> allDynamics = new HashSet<DynamicProperty>();
        for (String placeholder : placeholders) {
            String newValue = newProperties.getProperty(placeholder);
            String oldValue = oldProperties.getProperty(placeholder);
            if (newValue != null && !newValue.equals(oldValue) || newValue == null && oldValue != null) {
                if (logger.isInfoEnabled()) {
                    logger.info("Property changed detected: " + placeholder + (newValue != null ? "=" + newValue : " removed"));
                }
                List<DynamicProperty> affectedDynamics = placeholderToDynamics.get(placeholder);
                allDynamics.addAll(affectedDynamics);
            }
        }
        //
        // 获取受影响的beans
        //
        Map<String, List<DynamicProperty>> dynamicsByBeanName = new HashMap<String, List<DynamicProperty>>();
        Map<String, Object> beanByBeanName = new HashMap<String, Object>();
        for (DynamicProperty dynamic : allDynamics) {
            String beanName = dynamic.getBeanName();
            List<DynamicProperty> l = dynamicsByBeanName.get(beanName);
            if (l == null) {
                dynamicsByBeanName.put(beanName, (l = new ArrayList<DynamicProperty>()));
                Object bean = null;
                try {
                    bean = applicationContext.getBean(beanName);
                    beanByBeanName.put(beanName, bean);
                } catch (BeansException e) {
                    // keep dynamicsByBeanName list, warn only once.
                    logger.error("Error obtaining bean " + beanName, e);
                }
                //
                try {
                    if (bean instanceof IReconfigurationAware) {
                        // hello!
                        ((IReconfigurationAware) bean).beforeReconfiguration();
                    }
                } catch (Exception e) {
                    logger.error("Error calling beforeReconfiguration on " + beanName, e);
                }
            }
            l.add(dynamic);
        }
        //
        // 处理受影响的bean
        //
        Collection<String> beanNames = dynamicsByBeanName.keySet();
        for (String beanName : beanNames) {
            Object bean = beanByBeanName.get(beanName);
            if (// problems obtaining bean, earlier
            bean == null) {
                continue;
            }
            BeanWrapper beanWrapper = new BeanWrapperImpl(bean);
            // for all affected ...
            List<DynamicProperty> dynamics = dynamicsByBeanName.get(beanName);
            for (DynamicProperty dynamic : dynamics) {
                String propertyName = dynamic.getPropertyName();
                String unparsedValue = dynamic.getUnparsedValue();
                // obtain an updated value, including dependencies
                String newValue;
                removeDynamic(dynamic);
                currentBeanName = beanName;
                currentPropertyName = propertyName;
                try {
                    newValue = parseStringValue(unparsedValue, newProperties, new HashSet());
                } finally {
                    currentBeanName = null;
                    currentPropertyName = null;
                }
                if (logger.isInfoEnabled()) {
                    logger.info("Updating property " + beanName + "." + propertyName + " to " + newValue);
                }
                // assign it to the bean
                try {
                    beanWrapper.setPropertyValue(propertyName, newValue);
                } catch (BeansException e) {
                    logger.error("Error setting property " + beanName + "." + propertyName + " to " + newValue, e);
                }
            }
        }
        //
        for (String beanName : beanNames) {
            Object bean = beanByBeanName.get(beanName);
            try {
                if (bean instanceof IReconfigurationAware) {
                    ((IReconfigurationAware) bean).afterReconfiguration();
                }
            } catch (Exception e) {
                logger.error("Error calling afterReconfiguration on " + beanName, e);
            }
        }
    } catch (IOException e) {
        logger.error("Error trying to reload net.unicon.iamlabs.spring.properties.example.net.unicon.iamlabs" + ".spring" + ".properties: " + e.getMessage(), e);
    }
}
Also used : BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) HashMap(java.util.HashMap) IOException(java.io.IOException) Properties(java.util.Properties) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) BeanWrapper(org.springframework.beans.BeanWrapper) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) BeansException(org.springframework.beans.BeansException)

Example 57 with HashSet

use of java.util.HashSet in project eweb4j-framework by laiweiwei.

the class FileUtil method getJars.

public static Collection<String> getJars() {
    Collection<String> jars = new HashSet<String>();
    Enumeration<URL> urls;
    try {
        urls = FileUtil.class.getClassLoader().getResources("META-INF/MANIFEST.MF");
        while (urls.hasMoreElements()) {
            URL url = (URL) urls.nextElement();
            String path = url.getFile().replace("file:/", "").replace("!/META-INF/MANIFEST.MF", "");
            jars.add(CommonUtil.uriDecoding(path));
        }
        File jarDir = new File(getLib());
        if (jarDir.isDirectory() && jarDir.exists()) {
            for (File jar : jarDir.listFiles()) {
                jars.add(CommonUtil.uriDecoding(jar.getAbsolutePath()));
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return jars;
}
Also used : IOException(java.io.IOException) File(java.io.File) URL(java.net.URL) HashSet(java.util.HashSet)

Example 58 with HashSet

use of java.util.HashSet in project marine-api by ktuukkan.

the class SentenceReader method fireSentenceEvent.

/**
	 * Dispatch data to all listeners.
	 *
	 * @param sentence sentence string.
	 */
void fireSentenceEvent(Sentence sentence) {
    String type = sentence.getSentenceId();
    Set<SentenceListener> targets = new HashSet<SentenceListener>();
    if (listeners.containsKey(type)) {
        targets.addAll(listeners.get(type));
    }
    if (listeners.containsKey(DISPATCH_ALL)) {
        targets.addAll(listeners.get(DISPATCH_ALL));
    }
    for (SentenceListener listener : targets) {
        try {
            SentenceEvent se = new SentenceEvent(this, sentence);
            listener.sentenceRead(se);
        } catch (Exception e) {
            LOGGER.log(Level.WARNING, LOG_MSG, e);
        }
    }
}
Also used : SentenceEvent(net.sf.marineapi.nmea.event.SentenceEvent) SentenceListener(net.sf.marineapi.nmea.event.SentenceListener) HashSet(java.util.HashSet)

Example 59 with HashSet

use of java.util.HashSet in project blueprints by tinkerpop.

the class GraphSONUtilityTest method jsonFromElementVertexNoPropertiesWithKeysNoTypes.

@Test
public void jsonFromElementVertexNoPropertiesWithKeysNoTypes() throws JSONException {
    Vertex v = this.graph.addVertex(1);
    v.setProperty("x", "X");
    v.setProperty("y", "Y");
    v.setProperty("z", "Z");
    Set<String> propertiesToInclude = new HashSet<String>();
    propertiesToInclude.add("y");
    JSONObject json = GraphSONUtility.jsonFromElement(v, propertiesToInclude, GraphSONMode.NORMAL);
    Assert.assertNotNull(json);
    Assert.assertTrue(json.has(GraphSONTokens._ID));
    Assert.assertEquals(1, json.optInt(GraphSONTokens._ID));
    Assert.assertTrue(json.has(GraphSONTokens._TYPE));
    Assert.assertEquals("vertex", json.optString(GraphSONTokens._TYPE));
    Assert.assertFalse(json.has("x"));
    Assert.assertFalse(json.has("z"));
    Assert.assertTrue(json.has("y"));
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) JSONObject(org.codehaus.jettison.json.JSONObject) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 60 with HashSet

use of java.util.HashSet in project blueprints by tinkerpop.

the class GraphSONUtilityTest method jsonFromElementVertexIntIterablePropertiesNoKeysWithTypes.

@Test
public void jsonFromElementVertexIntIterablePropertiesNoKeysWithTypes() throws JSONException {
    Vertex v = this.graph.addVertex(1);
    Set<Integer> list = new HashSet<Integer>();
    list.add(0);
    list.add(1);
    list.add(2);
    v.setProperty("keyList", list);
    JSONObject json = GraphSONUtility.jsonFromElement(v, null, GraphSONMode.EXTENDED);
    Assert.assertNotNull(json);
    Assert.assertTrue(json.has(GraphSONTokens._ID));
    Assert.assertEquals(1, json.optInt(GraphSONTokens._ID));
    Assert.assertTrue(json.has("keyList"));
    JSONObject listWithTypeAsJson = json.optJSONObject("keyList");
    Assert.assertNotNull(listWithTypeAsJson);
    Assert.assertTrue(listWithTypeAsJson.has(GraphSONTokens.TYPE));
    Assert.assertEquals(GraphSONTokens.TYPE_LIST, listWithTypeAsJson.optString(GraphSONTokens.TYPE));
    Assert.assertTrue(listWithTypeAsJson.has(GraphSONTokens.VALUE));
    JSONArray listAsJSON = listWithTypeAsJson.optJSONArray(GraphSONTokens.VALUE);
    Assert.assertNotNull(listAsJSON);
    Assert.assertEquals(3, listAsJSON.length());
    for (int ix = 0; ix < listAsJSON.length(); ix++) {
        JSONObject valueAsJson = listAsJSON.optJSONObject(ix);
        Assert.assertNotNull(valueAsJson);
        Assert.assertTrue(valueAsJson.has(GraphSONTokens.TYPE));
        Assert.assertEquals(GraphSONTokens.TYPE_INTEGER, valueAsJson.optString(GraphSONTokens.TYPE));
        Assert.assertTrue(valueAsJson.has(GraphSONTokens.VALUE));
        Assert.assertEquals(ix, valueAsJson.optInt(GraphSONTokens.VALUE));
    }
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) JSONObject(org.codehaus.jettison.json.JSONObject) JSONArray(org.codehaus.jettison.json.JSONArray) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

HashSet (java.util.HashSet)13945 Set (java.util.Set)2858 ArrayList (java.util.ArrayList)2664 Test (org.junit.Test)2369 HashMap (java.util.HashMap)2357 Map (java.util.Map)1363 List (java.util.List)1033 IOException (java.io.IOException)1032 Iterator (java.util.Iterator)1030 File (java.io.File)684 LinkedHashSet (java.util.LinkedHashSet)561 Test (org.testng.annotations.Test)534 TreeSet (java.util.TreeSet)288 Collection (java.util.Collection)281 LinkedList (java.util.LinkedList)270 LinkedHashMap (java.util.LinkedHashMap)232 Region (org.apache.geode.cache.Region)202 Date (java.util.Date)195 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)192 Method (java.lang.reflect.Method)189