gluu
公開メンバ関数 | 静的公開変数類 | 非公開メンバ関数 | 非公開変数類 | 全メンバ一覧
org.gluu.credmanager.core.RSRegistryHandler クラス
org.gluu.credmanager.core.RSRegistryHandler 連携図
Collaboration graph

公開メンバ関数

void scan (String id, Path path, ClassLoader classLoader)
 
void remove (String id)
 

静的公開変数類

static final String ENDPOINTS_PREFIX = "pl"
 

非公開メンバ関数

void inited ()
 
boolean processClassEntry (String id, String binaryName, ClassLoader clsLoader, List< Class<?>> list)
 
int scanJarForRSResources (String id, JarInputStream inStream, ClassLoader clsLoader) throws IOException
 
int scanFolderForRSResources (String id, Path start, ClassLoader clsLoader) throws IOException
 

非公開変数類

Logger logger
 
ServletContext servletContext
 
Registry rsRegistry
 
boolean enabled
 
List< String > skipFolders
 
Map< String, List< Class<?> > > registeredResources
 

詳解

著者
jgomer

関数詳解

◆ inited()

void org.gluu.credmanager.core.RSRegistryHandler.inited ( )
inlineprivate
60  {
61  try {
62  skipFolders = Arrays.asList(ZKService.EXTERNAL_LABELS_DIR, ExtensionsManager.ASSETS_DIR, "META-INF");
63  registeredResources = new HashMap<>();
64 
65  //Try to find a ResteasyDeployment
66  Object obj = servletContext.getAttribute(Registry.class.getName());
67  //sc.getAttribute(ResteasyDeployment.class.getName())
68 
69  if (obj == null) {
70  obj = servletContext.getAttribute(ResteasyContextParameters.RESTEASY_DEPLOYMENTS);
71  Map<String, ResteasyDeployment> deployments = (Map<String, ResteasyDeployment>) obj;
72  rsRegistry = deployments.get(RSInitializer.ROOT_PATH).getRegistry();
73  } else {
74  rsRegistry = ((ResteasyDeployment) obj).getRegistry();
75  }
76 
77  } catch (Exception e) {
78  logger.error(e.getMessage(), e);
79  } finally {
80  if (rsRegistry == null) {
81  logger.warn("Could not access RestEasy registry. Addition of REST services at runtime may not be available");
82  } else {
83  logger.info("RestEasy registry is accessible. Addition of REST services at runtime will be available");
84  enabled = true;
85  }
86  }
87  //registry.removeRegistrations(RestService2.class);
88 
89  }
ServletContext servletContext
Definition: RSRegistryHandler.java:49
Logger logger
Definition: RSRegistryHandler.java:46
List< String > skipFolders
Definition: RSRegistryHandler.java:55
Registry rsRegistry
Definition: RSRegistryHandler.java:51
Map< String, List< Class<?> > > registeredResources
Definition: RSRegistryHandler.java:57
boolean enabled
Definition: RSRegistryHandler.java:53

◆ processClassEntry()

boolean org.gluu.credmanager.core.RSRegistryHandler.processClassEntry ( String  id,
String  binaryName,
ClassLoader  clsLoader,
List< Class<?>>  list 
)
inlineprivate
91  {
92 
93  boolean added = false;
94  try {
95  Class<?> cls = clsLoader.loadClass(binaryName);
96  javax.ws.rs.Path pathAnnotation = cls.getAnnotation(javax.ws.rs.Path.class);
97 
98  if (pathAnnotation != null) {
99  logger.info("Found class '{}' annotated with @Path", binaryName);
100  String basePath = ENDPOINTS_PREFIX + "/" + id;
101  String absolutePath = RSInitializer.ROOT_PATH + "/" + basePath + pathAnnotation.value();
102 
103  RSResourceScope scopeAnnotation = cls.getAnnotation(RSResourceScope.class);
104  boolean isSingleton = scopeAnnotation == null || scopeAnnotation.singleton();
105 
106  if (isSingleton) {
107  try {
108  rsRegistry.addSingletonResource(cls.newInstance(), basePath);
109  logger.info("Singleton resource has been bound to '{}' endpoint", absolutePath);
110  list.add(cls);
111  added = true;
112  } catch (Exception e) {
113  logger.error("Class could not be instantiated. Check it has no args constructor");
114  }
115  } else {
116  rsRegistry.addPerRequestResource(cls, basePath);
117  logger.info("Per-request resource has been bound to '{}' endpoint", absolutePath);
118  list.add(cls);
119  added = true;
120  }
121  }
122 
123  } catch (ClassNotFoundException e) {
124  //Intentionally left empty
125  }
126  return added;
127 
128  }
Logger logger
Definition: RSRegistryHandler.java:46
Registry rsRegistry
Definition: RSRegistryHandler.java:51
static final String ENDPOINTS_PREFIX
Definition: RSRegistryHandler.java:43

◆ remove()

void org.gluu.credmanager.core.RSRegistryHandler.remove ( String  id)
inline
207  {
208 
209  if (enabled) {
210  List<Class<?>> classes = registeredResources.get(id);
211  if (classes != null) {
212  for (Class<?> cls : classes) {
213  logger.debug("Removing RestEasy registration {}", cls.getName());
214  rsRegistry.removeRegistrations(cls, ENDPOINTS_PREFIX + "/" + id);
215  }
216  registeredResources.remove(id);
217  }
218  }
219 
220  }
Logger logger
Definition: RSRegistryHandler.java:46
Registry rsRegistry
Definition: RSRegistryHandler.java:51
Map< String, List< Class<?> > > registeredResources
Definition: RSRegistryHandler.java:57
boolean enabled
Definition: RSRegistryHandler.java:53
static final String ENDPOINTS_PREFIX
Definition: RSRegistryHandler.java:43

◆ scan()

void org.gluu.credmanager.core.RSRegistryHandler.scan ( String  id,
Path  path,
ClassLoader  classLoader 
)
inline
182  {
183 
184  int scannedResources = 0;
185 
186  if (enabled) {
187  try {
188  registeredResources.put(id, new ArrayList<>());
189  //Recursively scan for @Path annotation in directory (or jar file)
190  if (Files.isDirectory(path)) {
191  scannedResources = scanFolderForRSResources(id, path, classLoader);
192  } else if (Utils.isJarFile(path)) {
193  try (JarInputStream jis = new JarInputStream(new BufferedInputStream(new FileInputStream(path.toString())), false)) {
194  scannedResources = scanJarForRSResources(id, jis, classLoader);
195  }
196  }
197  } catch (IOException e) {
198  logger.error("Error scanning RestEasy resources: {}", e.getMessage());
199  }
200  logger.info("{} RestEasy resource class(es) registered", scannedResources);
201 
202  } else {
203  logger.info("Path '{}' will not be scanned: no RestEasy registry was found", path.toString());
204  }
205  }
Logger logger
Definition: RSRegistryHandler.java:46
Map< String, List< Class<?> > > registeredResources
Definition: RSRegistryHandler.java:57
boolean enabled
Definition: RSRegistryHandler.java:53
int scanJarForRSResources(String id, JarInputStream inStream, ClassLoader clsLoader)
Definition: RSRegistryHandler.java:130
int scanFolderForRSResources(String id, Path start, ClassLoader clsLoader)
Definition: RSRegistryHandler.java:150

◆ scanFolderForRSResources()

int org.gluu.credmanager.core.RSRegistryHandler.scanFolderForRSResources ( String  id,
Path  start,
ClassLoader  clsLoader 
) throws IOException
inlineprivate
150  {
151 
152  final class MyCounterFileVisitor extends SimpleFileVisitor<Path> {
153 
154  private int count = 0;
155  private int offset;
156 
157  private MyCounterFileVisitor(String basePath) {
158  this.offset = basePath.length() + 1; //Add one because trailing (back)slash is not in basePath
159  }
160 
161  @Override
162  public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
163 
164  String path = file.toString().substring(offset);
165  if (Utils.isClassFile(file) && skipFolders.stream().noneMatch(skip -> path.startsWith(skip + File.separator))) {
166  String binaryName = path.startsWith("classes" + File.separator) ? path.substring("classes".length() + 1) : path;
167  binaryName = binaryName.replace(File.separator, ".");
168  binaryName = binaryName.substring(0, binaryName.length() - ".class".length());
169  count += processClassEntry(id, binaryName, clsLoader, registeredResources.get(id)) ? 1 : 0;
170  }
171  return FileVisitResult.CONTINUE;
172  }
173 
174  }
175 
176  MyCounterFileVisitor visitor = new MyCounterFileVisitor(start.toString());
177  Files.walkFileTree(start, visitor);
178  return visitor.count;
179 
180  }
boolean processClassEntry(String id, String binaryName, ClassLoader clsLoader, List< Class<?>> list)
Definition: RSRegistryHandler.java:91
List< String > skipFolders
Definition: RSRegistryHandler.java:55
Map< String, List< Class<?> > > registeredResources
Definition: RSRegistryHandler.java:57

◆ scanJarForRSResources()

int org.gluu.credmanager.core.RSRegistryHandler.scanJarForRSResources ( String  id,
JarInputStream  inStream,
ClassLoader  clsLoader 
) throws IOException
inlineprivate
130  {
131 
132  int count = 0;
133  JarEntry entry = inStream.getNextJarEntry();
134 
135  for (; entry != null; entry = inStream.getNextJarEntry()) {
136  final String entryName = entry.getName();
137 
138  if (!entry.isDirectory() && entryName.endsWith(".class")
139  && skipFolders.stream().noneMatch(skip -> entryName.startsWith(skip + "/"))) {
140 
141  String binaryName = entryName.replace("/", ".");
142  binaryName = binaryName.substring(0, binaryName.length() - ".class".length());
143  count += processClassEntry(id, binaryName, clsLoader, registeredResources.get(id)) ? 1 : 0;
144  }
145  }
146  return count;
147 
148  }
boolean processClassEntry(String id, String binaryName, ClassLoader clsLoader, List< Class<?>> list)
Definition: RSRegistryHandler.java:91
List< String > skipFolders
Definition: RSRegistryHandler.java:55
Map< String, List< Class<?> > > registeredResources
Definition: RSRegistryHandler.java:57

メンバ詳解

◆ enabled

boolean org.gluu.credmanager.core.RSRegistryHandler.enabled
private

◆ ENDPOINTS_PREFIX

final String org.gluu.credmanager.core.RSRegistryHandler.ENDPOINTS_PREFIX = "pl"
static

◆ logger

Logger org.gluu.credmanager.core.RSRegistryHandler.logger
private

◆ registeredResources

Map<String, List<Class<?> > > org.gluu.credmanager.core.RSRegistryHandler.registeredResources
private

◆ rsRegistry

Registry org.gluu.credmanager.core.RSRegistryHandler.rsRegistry
private

◆ servletContext

ServletContext org.gluu.credmanager.core.RSRegistryHandler.servletContext
private

◆ skipFolders

List<String> org.gluu.credmanager.core.RSRegistryHandler.skipFolders
private

このクラス詳解は次のファイルから抽出されました: