Skip to content

Commit

Permalink
Format java files in o.e.equinox.servletbridge
Browse files Browse the repository at this point in the history
This was achieved by running:
eclipse -consolelog -nosplash -application org.eclipse.jdt.core.JavaCodeFormatter \
  -config .settings/org.eclipse.jdt.core.prefs . -data `mktemp -d`

Signed-off-by: Torbjörn SVENSSON <torbjorn.svensson@st.com>
  • Loading branch information
Torbjorn-Svensson authored and akurtakov committed Nov 4, 2023
1 parent 3e69d0d commit 6c186df
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
import javax.servlet.http.*;

/**
* The BridgeServlet provides a means to bridge the servlet and OSGi
* runtimes. This class has 3 main responsibilities:
* 1) Control the lifecycle of the associated FrameworkLauncher in line with its own lifecycle
* 2) Provide a servlet "hook" that allows all servlet requests to be delegated to the registered servlet
* 3) Provide means to manually control the framework lifecycle
* The BridgeServlet provides a means to bridge the servlet and OSGi runtimes.
* This class has 3 main responsibilities: 1) Control the lifecycle of the
* associated FrameworkLauncher in line with its own lifecycle 2) Provide a
* servlet "hook" that allows all servlet requests to be delegated to the
* registered servlet 3) Provide means to manually control the framework
* lifecycle
*/
public class BridgeServlet extends HttpServlet {

Expand All @@ -42,16 +43,18 @@ public class BridgeServlet extends HttpServlet {
private boolean enableFrameworkControls;

/**
* init() is called by the Servlet Container and used to instantiate the frameworkLauncher which MUST be an instance of FrameworkLauncher.
* After instantiating the framework init, deploy, and start are called.
* init() is called by the Servlet Container and used to instantiate the
* frameworkLauncher which MUST be an instance of FrameworkLauncher. After
* instantiating the framework init, deploy, and start are called.
*/
@Override
public void init() throws ServletException {
super.init();

enableFrameworkControls = Boolean.valueOf(getServletConfig().getInitParameter("enableFrameworkControls")); //$NON-NLS-1$

// Use with caution!! Some classes MUST be initialized with the web-app class loader
// Use with caution!! Some classes MUST be initialized with the web-app class
// loader
String frameworkPreloads = getServletConfig().getInitParameter("_contextPreloads"); //$NON-NLS-1$
if (frameworkPreloads != null) {
StringTokenizer st = new StringTokenizer(frameworkPreloads, ","); //$NON-NLS-1$
Expand All @@ -69,7 +72,8 @@ public void init() throws ServletException {
}

// Forces load of the SSLSocketFactory on the web-app context class loader
boolean initSSLSocketFactory = Optional.ofNullable(getServletConfig().getInitParameter("_initSSLSocketFactory")).map(Boolean::valueOf).orElse(true); //$NON-NLS-1$
boolean initSSLSocketFactory = Optional.ofNullable(getServletConfig().getInitParameter("_initSSLSocketFactory")) //$NON-NLS-1$
.map(Boolean::valueOf).orElse(true);
if (initSSLSocketFactory) {
try {
Class<?> clazz = this.getClass().getClassLoader().loadClass("javax.net.ssl.SSLSocketFactory"); //$NON-NLS-1$
Expand All @@ -84,7 +88,8 @@ public void init() throws ServletException {
String frameworkLauncherClassParameter = getServletConfig().getInitParameter("frameworkLauncherClass"); //$NON-NLS-1$
if (frameworkLauncherClassParameter != null) {
try {
Class<?> frameworkLauncherClass = this.getClass().getClassLoader().loadClass(frameworkLauncherClassParameter);
Class<?> frameworkLauncherClass = this.getClass().getClassLoader()
.loadClass(frameworkLauncherClassParameter);
framework = (FrameworkLauncher) frameworkLauncherClass.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new ServletException(e);
Expand All @@ -107,7 +112,8 @@ public void init() throws ServletException {
}

/**
* destroy() is called by the Servlet Container and used to first stop and then destroy the framework.
* destroy() is called by the Servlet Container and used to first stop and then
* destroy the framework.
*/
@Override
public void destroy() {
Expand All @@ -118,9 +124,10 @@ public void destroy() {
}

/**
* service is called by the Servlet Container and will first determine if the request is a
* framework control and will otherwise try to delegate to the registered servlet delegate
*
* service is called by the Servlet Container and will first determine if the
* request is a framework control and will otherwise try to delegate to the
* registered servlet delegate
*
*/
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Expand Down Expand Up @@ -175,12 +182,12 @@ private boolean isExtensionMapping(String servletPath) {
}

/**
* serviceFrameworkControls currently supports the following commands (identified by the request's pathinfo)
* sp_deploy - Copies the contents of the Equinox application to the install area
* sp_undeploy - Removes the copy of the Equinox application from the install area
* sp_redeploy - Resets the platform (e.g. stops, undeploys, deploys, starts)
* sp_start - Starts a deployed platform
* sp_stop - Stops the platform
* serviceFrameworkControls currently supports the following commands
* (identified by the request's pathinfo) sp_deploy - Copies the contents of the
* Equinox application to the install area sp_undeploy - Removes the copy of the
* Equinox application from the install area sp_redeploy - Resets the platform
* (e.g. stops, undeploys, deploys, starts) sp_start - Starts a deployed
* platform sp_stop - Stops the platform
*/
@SuppressWarnings("resource")
private boolean serviceFrameworkControls(HttpServletRequest req, HttpServletResponse resp) throws IOException {
Expand Down Expand Up @@ -241,10 +248,11 @@ private synchronized HttpServlet acquireDelegateReference() {
}

/**
* registerServletDelegate is the hook method called from inside the OSGi runtime to register
* a servlet for which all future servlet calls will be delegated. If not null and no delegate
* is currently registered, init(ServletConfig) will be called on the servletDelegate before
* returning.
* registerServletDelegate is the hook method called from inside the OSGi
* runtime to register a servlet for which all future servlet calls will be
* delegated. If not null and no delegate is currently registered,
* init(ServletConfig) will be called on the servletDelegate before returning.
*
* @param servletDelegate - the servlet to register for delegation
*/
public static synchronized void registerServletDelegate(HttpServlet servletDelegate) {
Expand All @@ -271,9 +279,12 @@ public static synchronized void registerServletDelegate(HttpServlet servletDeleg
}

/**
* unregisterServletDelegate is the hook method called from inside the OSGi runtime to unregister a delegate.
* If the servletDelegate matches the current registered delegate destroy() is called on the servletDelegate.
* destroy() will not be called until the delegate is finished servicing any previous requests.
* unregisterServletDelegate is the hook method called from inside the OSGi
* runtime to unregister a delegate. If the servletDelegate matches the current
* registered delegate destroy() is called on the servletDelegate. destroy()
* will not be called until the delegate is finished servicing any previous
* requests.
*
* @param servletDelegate - the servlet to unregister
*/
public static synchronized void unregisterServletDelegate(HttpServlet servletDelegate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public class CloseableURLClassLoader extends URLClassLoader {
static {
boolean registeredAsParallel;
try {
Method parallelCapableMetod = ClassLoader.class.getDeclaredMethod("registerAsParallelCapable", (Class[]) null); //$NON-NLS-1$
Method parallelCapableMetod = ClassLoader.class.getDeclaredMethod("registerAsParallelCapable", //$NON-NLS-1$
(Class[]) null);
parallelCapableMetod.setAccessible(true);
registeredAsParallel = ((Boolean) parallelCapableMetod.invoke(null, (Object[]) null)).booleanValue();
} catch (Throwable e) {
Expand Down Expand Up @@ -96,8 +97,7 @@ public InputStream getInputStream() throws IOException {
}

/**
* @throws IOException
* Documented to avoid warning
* @throws IOException Documented to avoid warning
*/
@Override
public JarFile getJarFile() throws IOException {
Expand Down Expand Up @@ -173,7 +173,7 @@ public CloseableURLClassLoader(URL[] urls) {
}

/**
* @param urls the URLs from which to load classes and resources
* @param urls the URLs from which to load classes and resources
* @param parent the parent class loader used for delegation
* @see URLClassLoader
*/
Expand All @@ -182,14 +182,16 @@ public CloseableURLClassLoader(URL[] urls, ClassLoader parent) {
}

/**
* @param urls the URLs from which to load classes and resources
* @param parent the parent class loader used for delegation
* @param verifyJars flag to determine if jar file verification should be performed
* @param urls the URLs from which to load classes and resources
* @param parent the parent class loader used for delegation
* @param verifyJars flag to determine if jar file verification should be
* performed
* @see URLClassLoader
*/
public CloseableURLClassLoader(URL[] urls, ClassLoader parent, boolean verifyJars) {
super(excludeFileJarURLS(urls), parent);
this.registeredAsParallel = CLOSEABLE_REGISTERED_AS_PARALLEL && this.getClass() == CloseableURLClassLoader.class;
this.registeredAsParallel = CLOSEABLE_REGISTERED_AS_PARALLEL
&& this.getClass() == CloseableURLClassLoader.class;
this.context = AccessController.getContext();
this.verifyJars = verifyJars;
for (URL url : urls) {
Expand All @@ -202,7 +204,8 @@ public CloseableURLClassLoader(URL[] urls, ClassLoader parent, boolean verifyJar

// @GuardedBy("loaders")
private boolean safeAddLoader(URL url) {
//assume all illegal characters have been properly encoded, so use URI class to unencode
// assume all illegal characters have been properly encoded, so use URI class to
// unencode
try {
File file = new File(toURI(url));
if (file.exists()) {
Expand All @@ -224,13 +227,15 @@ private static URI toURI(URL url) throws URISyntaxException {
if (!SCHEME_FILE.equals(url.getProtocol())) {
throw new IllegalArgumentException("bad prototcol: " + url.getProtocol()); //$NON-NLS-1$
}
//URL behaves differently across platforms so for file: URLs we parse from string form
// URL behaves differently across platforms so for file: URLs we parse from
// string form
String pathString = url.toExternalForm().substring(5);
//ensure there is a leading slash to handle common malformed URLs such as file:c:/tmp
// ensure there is a leading slash to handle common malformed URLs such as
// file:c:/tmp
if (pathString.indexOf('/') != 0)
pathString = '/' + pathString;
else if (pathString.startsWith(UNC_PREFIX) && !pathString.startsWith(UNC_PREFIX, 2)) {
//URL encodes UNC path with two slashes, but URI uses four (see bug 207103)
// URL encodes UNC path with two slashes, but URI uses four (see bug 207103)
pathString = ensureUNCPath(pathString);
}
return new URI(SCHEME_FILE, null, pathString, null);
Expand All @@ -243,7 +248,7 @@ private static String ensureUNCPath(String path) {
int len = path.length();
StringBuilder result = new StringBuilder(len);
for (int i = 0; i < 4; i++) {
// if we have hit the first non-slash character, add another leading slash
// if we have hit the first non-slash character, add another leading slash
if (i >= len || result.length() > 0 || path.charAt(i) != '/')
result.append('/');
}
Expand Down Expand Up @@ -362,7 +367,8 @@ private void checkForSealedPackage(Package pkg, String packageName, Manifest man
// previously sealed case
if (!pkg.isSealed(jarFileURL)) {
// this URL does not seal; ERROR
throw new SecurityException("The package '" + packageName + "' was previously loaded and is already sealed."); //$NON-NLS-1$ //$NON-NLS-2$
throw new SecurityException(
"The package '" + packageName + "' was previously loaded and is already sealed."); //$NON-NLS-1$ //$NON-NLS-2$
}
} else {
// previously unsealed case
Expand All @@ -378,8 +384,10 @@ private void checkForSealedPackage(Package pkg, String packageName, Manifest man
sealed = mainAttributes.getValue(Name.SEALED);
}
if (Boolean.valueOf(sealed).booleanValue()) {
// this manifest attempts to seal when package defined previously unsealed; ERROR
throw new SecurityException("The package '" + packageName + "' was previously loaded unsealed. Cannot seal package."); //$NON-NLS-1$ //$NON-NLS-2$
// this manifest attempts to seal when package defined previously unsealed;
// ERROR
throw new SecurityException(
"The package '" + packageName + "' was previously loaded unsealed. Cannot seal package."); //$NON-NLS-1$ //$NON-NLS-2$
}
}
}
Expand Down Expand Up @@ -432,8 +440,9 @@ public Object run() {
}

/**
* The "close" method is called when the class loader is no longer needed and we should close any open resources.
* In particular this method will close the jar files associated with this class loader.
* The "close" method is called when the class loader is no longer needed and we
* should close any open resources. In particular this method will close the jar
* files associated with this class loader.
*/
@Override
public void close() {
Expand Down
Loading

0 comments on commit 6c186df

Please sign in to comment.