public void addToCache( Class<? extends Plugin> plugin, final RemoteManagerEndpoint manager, final DiscoveredPlugin src) { synchronized (this.cache) { // 1) get all managers providing this plugin HashMap<RemoteManagerEndpoint, Entry> managers = this.cache.get(plugin); if (null == managers) { // if we dont have one, so create managers = new HashMap<RemoteManagerEndpoint, Entry>(); managers.put(manager, new Entry(plugin.getCanonicalName(), manager, src)); this.cache.put(plugin, managers); return; } // 2) get the entry providing this plugin. Entry entry = managers.get(manager); if (null == entry) { // no entry managers.put(manager, new Entry(plugin.getCanonicalName(), manager, src)); return; } // 3) here it means we have already an entry... so skip!!! } }
/** * Overwrite this method if you want to filter the input, apply hashing, etc. * * @param feature the current feature. * @param document the current document. * @param featureFieldName the field hashFunctionsFileName of the feature. */ protected void addToDocument(LireFeature feature, Document document, String featureFieldName) { if (run == 0) { } // just count documents else if (run == 1) { // Select the representatives ... if (representativesID.contains(docCount) && feature .getClass() .getCanonicalName() .equals(featureClass.getCanonicalName())) { // it's a representative. // put it into a temporary data structure ... representatives.add(feature); } } else if (run == 2) { // actual hashing: find the nearest representatives and put those as a hash into a // document. if (feature .getClass() .getCanonicalName() .equals(featureClass.getCanonicalName())) { // it's a feature to be hashed int[] hashes = getHashes(feature); document.add( new TextField( featureFieldName + "_hash", createDocumentString(hashes, hashes.length), Field.Store.YES)); document.add( new TextField( featureFieldName + "_hash_q", createDocumentString(hashes, 10), Field.Store.YES)); } document.add(new StoredField(featureFieldName, feature.getByteArrayRepresentation())); } }
@SuppressWarnings({"boxing", "unchecked"}) public void loadCache() { this.cache.clear(); try { ObjectInputStream in = new ObjectInputStream(new FileInputStream(CACHE_FILE)); this.remoteDiscoveryImpl.syso("load cache"); try { final int cacheSize = (Integer) in.readObject(); // syso("size "+cacheSize); for (int i = 0; i < cacheSize; ++i) { HashMap<RemoteManagerEndpoint, Entry> rmee = new HashMap<RemoteManagerEndpoint, Entry>(); Class<? extends Plugin> plugin = (Class<? extends Plugin>) in.readObject(); this.remoteDiscoveryImpl.syso(plugin.getCanonicalName()); // syso("\t"+i+"'"+pluginName+"'"); int numEntries = (Integer) in.readObject(); // syso("\t"+numEntries); for (int j = 0; j < numEntries; ++j) { Entry entry = (Entry) in.readObject(); // syso("\t\t"+entry); rmee.put(entry.manager, entry); } this.cache.put(plugin, rmee); } } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { in.close(); } } catch (FileNotFoundException e) { this.remoteDiscoveryImpl.logger.warning("Loading cache file" + CACHE_FILE + " failed."); } catch (IOException e) { e.printStackTrace(); } }
public static Properties loadProperties(Class<?> clazz, String name, String extraProperty) throws IOException { Closer closer = Closer.create(); Properties prop = new Properties(); try { InputStream in = closer.register(clazz.getResourceAsStream(name)); prop.load(in); String extraPath = System.getProperty(extraProperty); if (extraPath != null) { log.info( "Loading extra properties for " + clazz.getCanonicalName() + ":" + name + " from " + extraPath + "..."); in = closer.register( new BufferedInputStream(closer.register(new FileInputStream(extraPath)))); prop.load(in); } } finally { closer.close(); } return prop; }
/** Reflect operations demo */ public static void reflect(Object obj) { // `cls用于描述对象所属的类` Class cls = obj.getClass(); print("Class Name: " + cls.getCanonicalName()); // `fields包含对象的所有属性` Field[] fields = cls.getDeclaredFields(); print("List of fields:"); for (Field f : fields) { print(String.format("%30s %15s", f.getType(), f.getName())); } // `methods包含对象的所有方法` Method[] methods = cls.getDeclaredMethods(); print("List of methods:"); for (Method m : methods) print( String.format( "%30s %15s %30s", m.getReturnType(), m.getName(), Arrays.toString(m.getParameterTypes()))); Constructor[] constructors = cls.getConstructors(); print("List of contructors:"); for (Constructor c : constructors) print(String.format("%30s %15s", c.getName(), Arrays.toString(c.getParameterTypes()))); }
private String pathOf(Class<?> api) { return api.getCanonicalName().replace('.', '/'); }