1 16 package org.apache.pluto; 17 18 import java.util.HashMap ; 19 import java.util.Stack ; 20 21 import org.apache.pluto.services.ContainerService; 22 import org.apache.pluto.services.PortletContainerEnvironment; 23 24 27 public class PortletContainerServices 28 { 29 30 public static ContainerService get(Class service) 31 { 32 Stack currentContainerServiceStack = (Stack )currentContainerService.get(); 35 36 if (currentContainerServiceStack.isEmpty()) 37 { 38 throw new IllegalStateException ("The prepare method was never called"); 39 } 40 41 PortletContainerEnvironment environment = (PortletContainerEnvironment)currentContainerServiceStack.peek(); 42 43 if (environment == null) 44 { 45 throw new IllegalStateException ("The prepare method was never called"); 46 } 47 48 return environment.getContainerService(service); 49 } 50 51 public static String getUniqueContainerName() 52 { 53 Stack currentNameStack = (Stack )currentUniqueContainerName.get(); 55 56 if (currentNameStack.isEmpty()) 57 { 58 throw new IllegalStateException ("The prepare method was never called"); 59 } 60 61 return(String )currentNameStack.peek(); 62 } 63 64 synchronized public static void createReference(String uniqueContainerName, PortletContainerEnvironment environment) 65 { 66 if (containerServices.containsKey(uniqueContainerName)) 67 { 68 throw new IllegalArgumentException ("The given container name is not unique: "+uniqueContainerName); 69 } 70 71 containerServices.put(uniqueContainerName, environment); 72 } 73 74 synchronized public static void destroyReference(String uniqueContainerName) 75 { 76 containerServices.remove(uniqueContainerName); 78 } 79 80 84 synchronized public static void prepare(String uniqueContainerName) 85 { 86 Stack currentContainerServiceStack = (Stack )currentContainerService.get(); 88 currentContainerServiceStack.push(containerServices.get(uniqueContainerName)); 90 91 Stack currentNameStack = (Stack )currentUniqueContainerName.get(); 93 currentNameStack.push(uniqueContainerName); 95 } 96 97 100 public static void release() 101 { 102 Stack currentContainerServiceStack = (Stack )currentContainerService.get(); 105 if (!currentContainerServiceStack.isEmpty()) 106 { 107 currentContainerServiceStack.pop(); 108 } 109 110 Stack currentNameStack = (Stack )currentUniqueContainerName.get(); 113 if (!currentNameStack.isEmpty()) 114 { 115 currentNameStack.pop(); 116 } 117 } 118 119 private static ThreadLocal currentContainerService = new StackedThreadLocal(); 121 private static ThreadLocal currentUniqueContainerName = new StackedThreadLocal(); 123 124 private static HashMap containerServices = new HashMap (); 126 127 128 129 private static class StackedThreadLocal extends ThreadLocal 130 { 131 public Object initialValue() 132 { 133 return new Stack (); 134 } 135 } 136 137 } 138 | Popular Tags |