`AbstractAddonManager.java`
`001 /* 002 * Copyright 2009-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.codehaus.griffon.runtime.core; 018 019 import griffon.core.AddonManager; 020 import griffon.core.GriffonAddon; 021 import griffon.core.GriffonAddonDescriptor; 022 import griffon.core.GriffonApplication; 023 import griffon.util.GriffonNameUtils; 024 import org.slf4j.Logger; 025 import org.slf4j.LoggerFactory; 026 027 import java.util.Collections; 028 import java.util.LinkedHashMap; 029 import java.util.Map; 030 031 /** 032 * Base implementation of the {@code AddonManager} interface. 033 * 034 * @author Andres Almiray 035 * @since 0.9.2 036 */ 037 public abstract class AbstractAddonManager implements AddonManager { 038 private final GriffonApplication app; 039 040 private final Map<String, GriffonAddon> addons = new LinkedHashMap<String, GriffonAddon>(); 041 private final Map<String, GriffonAddonDescriptor> addonDescriptors = new LinkedHashMap<String, GriffonAddonDescriptor>(); 042 private final Object lock = new Object(); 043 private boolean initialized; 044 045 private static final Logger LOG = LoggerFactory.getLogger(AbstractAddonManager.class); 046 047 public AbstractAddonManager(GriffonApplication app) { 048 this.app = app; 049 } 050 051 public GriffonApplication getApp() { 052 return app; 053 } 054 055 public Map<String, GriffonAddon> getAddons() { 056 return Collections.<String, GriffonAddon>unmodifiableMap(addons); 057 } 058 059 public Map<String, GriffonAddonDescriptor> getAddonDescriptors() { 060 return Collections.<String, GriffonAddonDescriptor>unmodifiableMap(addonDescriptors); 061 } 062 063 public GriffonAddon findAddon(String name) { 064 if (GriffonNameUtils.isBlank(name)) return null; 065 if (name.endsWith("GriffonAddon")) name = name.substring(0, name.length() - 12); 066 return addons.get(GriffonNameUtils.getPropertyName(name)); 067 } 068 069 public GriffonAddonDescriptor findAddonDescriptor(String name) { 070 if (GriffonNameUtils.isBlank(name)) return null; 071 if (name.endsWith("GriffonAddon")) name = name.substring(0, name.length() - 12); 072 return addonDescriptors.get(GriffonNameUtils.getPropertyName(name)); 073 } 074 075 public Map<String, GriffonAddonDescriptor> findAddonDescriptors(String prefix) { 076 if ("root".equals(prefix) || GriffonNameUtils.isBlank(prefix)) prefix = ""; 077 078 Map<String, GriffonAddonDescriptor> map = new LinkedHashMap<String, GriffonAddonDescriptor>(); 079 for (Map.Entry<String, GriffonAddonDescriptor> entry : addonDescriptors.entrySet()) { 080 if (prefix.equals(entry.getValue().getPrefix())) { 081 map.put(entry.getKey(), entry.getValue()); 082 } 083 } 084 085 return map; 086 } 087 088 public final void registerAddon(GriffonAddonDescriptor addonDescriptor) { 089 synchronized (lock) { 090 if (!initialized) { 091 LOG.debug("Registering addon descriptor " + addonDescriptor); 092 doRegisterAddon(addonDescriptor); 093 addons.put(addonDescriptor.getPluginName(), addonDescriptor.getAddon()); 094 } 095 } 096 } 097 098 public final void initialize() { 099 synchronized (lock) { 100 if (!initialized) { 101 doInitialize(); 102 initialized = true; 103 } 104 } 105 } 106 107 protected abstract void doInitialize(); 108 109 protected abstract void doRegisterAddon(GriffonAddonDescriptor addonDescriptor); 110 111 protected Map<String, GriffonAddonDescriptor> getAddonsInternal() { 112 return addonDescriptors; 113 } 114 }`