/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

  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. */
  23. package com.bluemarsh.jswat.core.path;
  24. import com.bluemarsh.jswat.core.session.Session;
  25. import com.bluemarsh.jswat.core.session.SessionListener;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. import java.util.logging.Level;
  29. import java.util.logging.Logger;
  30. import org.openide.util.Lookup;
  31. /**
  32. * Class PathProvider manages a set of PathManager instances, one for each
  33. * unique Session passed to the <code>getPathManager()</code> method.
  34. *
  35. * @author Nathan Fiedler
  36. */
  37. public class PathProvider {
  38. /** Logger for gracefully reporting unexpected errors. */
  39. private static final Logger logger = Logger.getLogger(
  40. PathProvider.class.getName());
  41. /** Used to control access to the instance maps. */
  42. private static final Object mapsLock;
  43. /** Map of PathManager instances, keyed by Session instance. */
  44. private static Map<Session, PathManager> instanceMap;
  45. /** Map of Session instances, keyed by PathManager instance. */
  46. private static Map<PathManager, Session> reverseMap;
  47. static {
  48. mapsLock = new Object();
  49. instanceMap = new HashMap<Session, PathManager>();
  50. reverseMap = new HashMap<PathManager, Session>();
  51. }
  52. /**
  53. * Creates a new instance of PathProvider.
  54. */
  55. private PathProvider() {
  56. }
  57. /**
  58. * Retrieve the PathManager instance for the given Session, creating
  59. * one if necessary.
  60. *
  61. * @param session Session for which to get PathManager.
  62. * @return path manager instance.
  63. */
  64. public static PathManager getPathManager(Session session) {
  65. synchronized (mapsLock) {
  66. PathManager inst = instanceMap.get(session);
  67. if (inst == null) {
  68. // Perform lookup to find a PathManager instance.
  69. PathManager prototype = Lookup.getDefault().lookup(PathManager.class);
  70. // Using this prototype, construct a new instance for the
  71. // given Session, rather than sharing the single instance.
  72. Class<? extends PathManager> protoClass = prototype.getClass();
  73. try {
  74. inst = protoClass.newInstance();
  75. } catch (InstantiationException ie) {
  76. logger.log(Level.SEVERE, null, ie);
  77. return null;
  78. } catch (IllegalAccessException iae) {
  79. logger.log(Level.SEVERE, null, iae);
  80. return null;
  81. }
  82. instanceMap.put(session, inst);
  83. reverseMap.put(inst, session);
  84. if (inst instanceof SessionListener) {
  85. session.addSessionListener((SessionListener) inst);
  86. }
  87. }
  88. return inst;
  89. }
  90. }
  91. /**
  92. * Retrieve the Session instance associated with the given PathManager.
  93. *
  94. * @param pm PathManager for which to find Session.
  95. * @return Session, or null if none is mapped to the given PathManager.
  96. */
  97. public static Session getSession(PathManager pm) {
  98. synchronized (mapsLock) {
  99. return reverseMap.get(pm);
  100. }
  101. }
  102. }