/** * Searches for a module matching the supplied URN. if a module instance that matches the supplied * URN exactly is found it is returned. * * <p>Otherwise, an attempt is made to find the first module that has the same URN elements as the * ones that are specified in the supplied URN and return it. * * <p>For example, if the supplied URN is <code>metc:mytype</code>, this method will return the * first module that is found to have a URN with the providerType 'mytype'. * * <p>Do note that this method doesn't do implement an efficient search as it iterates through the * URNs of all the modules. This may not be a huge issue, if the number of modules is small. * However, if the number of modules grows, we might want to use different data structures to make * the search more efficient. * * @param inURN the module URN * @return a module matching the supplied URN, null if no matching module could be found * @throws ModuleNotFoundException if multiple modules matching the supplied URN were found. */ synchronized Module search(ModuleURN inURN) throws ModuleNotFoundException { Module m; // Look for an exact match m = get(inURN); if (m != null) { return m; } // Figure out which elements of the URN are specified. boolean noProviderType = inURN.providerType() == null || inURN.providerType().isEmpty(); boolean noProviderName = inURN.providerName() == null || inURN.providerName().isEmpty(); boolean noInstanceName = inURN.instanceName() == null || inURN.instanceName().isEmpty(); // Match the first instance that matches all the specified fields // of the URN Module returnValue = null; for (ModuleURN i : mModules.keySet()) { if ((noProviderType || inURN.providerType().equals(i.providerType())) && (noProviderName || inURN.providerName().equals(i.providerName())) && (noInstanceName || inURN.instanceName().equals(i.instanceName()))) { if (returnValue == null) { returnValue = get(i); } else { throw new ModuleNotFoundException( new I18NBoundMessage3P( Messages.MULTIPLE_MODULES_MATCH_URN, inURN.getValue(), returnValue.getURN().getValue(), get(i).getURN().getValue())); } } } return returnValue; }