001/* 002 * CDDL HEADER START 003 * 004 * The contents of this file are subject to the terms of the 005 * Common Development and Distribution License, Version 1.0 only 006 * (the "License"). You may not use this file except in compliance 007 * with the License. 008 * 009 * You can obtain a copy of the license at 010 * docs/licenses/cddl.txt 011 * or http://www.opensource.org/licenses/cddl1.php. 012 * See the License for the specific language governing permissions 013 * and limitations under the License. 014 * 015 * When distributing Covered Code, include this CDDL HEADER in each 016 * file and include the License file at 017 * docs/licenses/cddl.txt. If applicable, 018 * add the following below this CDDL HEADER, with the fields enclosed 019 * by brackets "[]" replaced with your own identifying information: 020 * Portions Copyright [yyyy] [name of copyright owner] 021 * 022 * CDDL HEADER END 023 * 024 * 025 * Copyright 2012-2019 Ping Identity Corporation 026 */ 027package com.unboundid.directory.sdk.http.types; 028 029 030 031import java.lang.reflect.Constructor; 032 033import com.unboundid.util.StaticUtils; 034import com.unboundid.util.ThreadSafety; 035import com.unboundid.util.ThreadSafetyLevel; 036 037 038 039/** 040 * This class provides a factory which may be used to obtain an of a 041 * {@link WebAppServerContext} object. 042 */ 043@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 044public final class WebAppServerContextFactory 045{ 046 /** 047 * Obtain an instance of a {@link WebAppServerContext} object. Note that this 048 * may only be called by a web application running inside the server. 049 * 050 * @return An instance of a {@code WebAppServerContext} object. 051 */ 052 public static WebAppServerContext getServerContext() 053 { 054 try 055 { 056 final Class<?> genericProviderClass = Class.forName( 057 "com.unboundid.directory.sdk.impl.ServerContextProvider"); 058 final Class<?> specificProviderClass = Class.forName( 059 "com.unboundid.directory.sdk.impl.WebAppServerContextProvider"); 060 061 final Constructor<?> providerClassConstructor = 062 specificProviderClass.getDeclaredConstructor(); 063 providerClassConstructor.setAccessible(true); 064 final Object providerObject = providerClassConstructor.newInstance(); 065 066 final Class<?> implClass = Class.forName( 067 "com.unboundid.directory.sdk.impl.ServerContextImpl"); 068 069 final Constructor<?> constructor = 070 implClass.getConstructor(genericProviderClass); 071 constructor.setAccessible(true); 072 073 final Object o = constructor.newInstance(providerObject); 074 return (WebAppServerContext) o; 075 } 076 catch (final Exception e) 077 { 078 throw new RuntimeException( 079 "Unable to obtain an instance of a WebAppServerContext: " + 080 StaticUtils.getExceptionMessage(e), 081 e); 082 } 083 } 084}