PageRenderTime 15ms CodeModel.GetById 6ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/core/src/com/bluemarsh/jswat/core/path/PathProvider.java

http://jswat.googlecode.com/
Java | 109 lines | 51 code | 7 blank | 51 comment | 3 complexity | 3b536c93ab80393202eb8b63739201da MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1/*
  2 * The contents of this file are subject to the terms of the Common Development
  3 * and Distribution License (the License). You may not use this file except in
  4 * compliance with the License.
  5 *
  6 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
  7 * or http://www.netbeans.org/cddl.txt.
  8 *
  9 * When distributing Covered Code, include this CDDL Header Notice in each file
 10 * and include the License file at http://www.netbeans.org/cddl.txt.
 11 * If applicable, add the following below the CDDL Header, with the fields
 12 * enclosed by brackets [] replaced by your own identifying information:
 13 * "Portions Copyrighted [year] [name of copyright owner]"
 14 *
 15 * The Original Software is JSwat. The Initial Developer of the Original
 16 * Software is Nathan L. Fiedler. Portions created by Nathan L. Fiedler
 17 * are Copyright (C) 2005-2010. All Rights Reserved.
 18 *
 19 * Contributor(s): Nathan L. Fiedler.
 20 *
 21 * $Id: PathProvider.java 285 2010-11-20 23:56:08Z nathanfiedler $
 22 */
 23package com.bluemarsh.jswat.core.path;
 24
 25import com.bluemarsh.jswat.core.session.Session;
 26import com.bluemarsh.jswat.core.session.SessionListener;
 27import java.util.HashMap;
 28import java.util.Map;
 29import java.util.logging.Level;
 30import java.util.logging.Logger;
 31import org.openide.util.Lookup;
 32
 33/**
 34 * Class PathProvider manages a set of PathManager instances, one for each
 35 * unique Session passed to the <code>getPathManager()</code> method.
 36 *
 37 * @author Nathan Fiedler
 38 */
 39public class PathProvider {
 40
 41    /** Logger for gracefully reporting unexpected errors. */
 42    private static final Logger logger = Logger.getLogger(
 43            PathProvider.class.getName());
 44    /** Used to control access to the instance maps. */
 45    private static final Object mapsLock;
 46    /** Map of PathManager instances, keyed by Session instance. */
 47    private static Map<Session, PathManager> instanceMap;
 48    /** Map of Session instances, keyed by PathManager instance. */
 49    private static Map<PathManager, Session> reverseMap;
 50
 51    static {
 52        mapsLock = new Object();
 53        instanceMap = new HashMap<Session, PathManager>();
 54        reverseMap = new HashMap<PathManager, Session>();
 55    }
 56
 57    /**
 58     * Creates a new instance of PathProvider.
 59     */
 60    private PathProvider() {
 61    }
 62
 63    /**
 64     * Retrieve the PathManager instance for the given Session, creating
 65     * one if necessary.
 66     *
 67     * @param  session  Session for which to get PathManager.
 68     * @return  path manager instance.
 69     */
 70    public static PathManager getPathManager(Session session) {
 71        synchronized (mapsLock) {
 72            PathManager inst = instanceMap.get(session);
 73            if (inst == null) {
 74                // Perform lookup to find a PathManager instance.
 75                PathManager prototype = Lookup.getDefault().lookup(PathManager.class);
 76                // Using this prototype, construct a new instance for the
 77                // given Session, rather than sharing the single instance.
 78                Class<? extends PathManager> protoClass = prototype.getClass();
 79                try {
 80                    inst = protoClass.newInstance();
 81                } catch (InstantiationException ie) {
 82                    logger.log(Level.SEVERE, null, ie);
 83                    return null;
 84                } catch (IllegalAccessException iae) {
 85                    logger.log(Level.SEVERE, null, iae);
 86                    return null;
 87                }
 88                instanceMap.put(session, inst);
 89                reverseMap.put(inst, session);
 90                if (inst instanceof SessionListener) {
 91                    session.addSessionListener((SessionListener) inst);
 92                }
 93            }
 94            return inst;
 95        }
 96    }
 97
 98    /**
 99     * Retrieve the Session instance associated with the given PathManager.
100     *
101     * @param  pm  PathManager for which to find Session.
102     * @return  Session, or null if none is mapped to the given PathManager.
103     */
104    public static Session getSession(PathManager pm) {
105        synchronized (mapsLock) {
106            return reverseMap.get(pm);
107        }
108    }
109}