001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.struts;
016    
017    import com.liferay.portal.kernel.struts.StrutsAction;
018    import com.liferay.portal.kernel.struts.StrutsPortletAction;
019    
020    import java.util.HashMap;
021    import java.util.Map;
022    import java.util.concurrent.ConcurrentHashMap;
023    
024    import org.apache.struts.action.Action;
025    import org.apache.struts.util.WildcardHelper;
026    
027    /**
028     * @author Mika Koivisto
029     * @author Raymond Aug??
030     */
031    public class StrutsActionRegistryImpl implements StrutsActionRegistry {
032    
033            @Override
034            public Action getAction(String path) {
035                    Action action = _actions.get(path);
036    
037                    if (action != null) {
038                            return action;
039                    }
040    
041                    Map<String, String> matchesMap = new HashMap<String, String>();
042    
043                    for (Map.Entry<String, Action> entry : _actions.entrySet()) {
044                            int[] pattern = _patterns.get(entry.getKey());
045    
046                            if (_wildcardHelper.match(matchesMap, path, pattern)) {
047                                    return entry.getValue();
048                            }
049                    }
050    
051                    return null;
052            }
053    
054            @Override
055            public Map<String, Action> getActions() {
056                    return _actions;
057            }
058    
059            @Override
060            public void register(String path, StrutsAction strutsAction) {
061                    Action action = new ActionAdapter(strutsAction);
062    
063                    _actions.put(path, action);
064    
065                    _patterns.put(path, _wildcardHelper.compilePattern(path));
066            }
067    
068            @Override
069            public void register(String path, StrutsPortletAction strutsPortletAction) {
070                    Action action = new PortletActionAdapter(strutsPortletAction);
071    
072                    _actions.put(path, action);
073    
074                    _patterns.put(path, _wildcardHelper.compilePattern(path));
075            }
076    
077            @Override
078            public void unregister(String path) {
079                    _actions.remove(path);
080    
081                    _patterns.remove(path);
082            }
083    
084            private static Map<String, Action> _actions =
085                    new ConcurrentHashMap<String, Action>();
086            private static Map<String, int[]> _patterns =
087                    new ConcurrentHashMap<String, int[]>();
088            private static WildcardHelper _wildcardHelper = new WildcardHelper();
089    
090    }