|
Glassfish example source code file (JWSACCMain.java)
The Glassfish JWSACCMain.java source code/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License"). You * may not use this file except in compliance with the License. You can * obtain a copy of the License at * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html * or packager/legal/LICENSE.txt. See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at packager/legal/LICENSE.txt. * * GPL Classpath Exception: * Oracle designates this particular file as subject to the "Classpath" * exception as provided by Oracle in the GPL Version 2 section of the License * file that accompanied this code. * * Modifications: * If applicable, add the following below the License Header, with the fields * enclosed by brackets [] replaced by your own identifying information: * "Portions Copyright [year] [name of copyright owner]" * * Contributor(s): * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license." If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above. However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. */ package org.glassfish.appclient.client.jws.boot; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.net.URLClassLoader; import java.security.Policy; import java.text.MessageFormat; import java.util.ResourceBundle; import java.util.Vector; import javax.swing.SwingUtilities; import org.glassfish.appclient.client.acc.AppClientContainer; import org.glassfish.appclient.common.Util; /** *Alternate main class for ACC, used when launched by Java Web Start. *<p> *This class assigns security permissions needed by the app server code and *by the app client code, then starts the regular app client container. *<p> *Note that any logic this class executes that requires privileged access *must occur either: *- from a class in the signed jar containing this class, or *- after setPermissions has been invoked. *This is because Java Web Start grants elevated permissions only to the classes *in the appserv-jwsacc-signed.jar at the beginning. Only after setPermissions *has been invoked can other app server-provided code run with all permissions. * * @author tjquinn */ public class JWSACCMain implements Runnable { // /** path to a class in one of the app server lib jars downloaded by Java Web Start */ // private static final String APPSERVER_LIB_CLASS_NAME = "com.sun.enterprise.server.ApplicationServer"; /** name of the permissions template */ private static final String PERMISSIONS_TEMPLATE_NAME = "jwsclient.policy"; /** placeholder used in the policy template to substitute dynamically-generated grant clauses */ private static final String GRANT_CLAUSES_PROPERTY_EXPR = "${grant.clauses}"; /** line separator */ private static final String lineSep = System.getProperty("line.separator"); /** the user-specified security policy template to use */ private static String jwsPolicyTemplateURL = null; /** unpublished command-line argument conveying jwsacc information */ private static final String JWSACC_ARGUMENT_PREFIX = "-jwsacc"; private static final String JWSACC_EXIT_AFTER_RETURN = "ExitAfterReturn"; private static final String JWSACC_FORCE_ERROR = "ForceError"; private static final String JWSACC_KEEP_JWS_CLASS_LOADER = "KeepJWSClassLoader"; private static final String JWSACC_RUN_ON_SWING_THREAD = "RunOnSwingThread"; /** grant clause template for dynamically populating the policy */ private static final String GRANT_CLAUSE_TEMPLATE = "grant codeBase \"{0}\" '{'\n" + " permission java.security.AllPermission;\n" + "'}';"; /** * request to exit the JVM upon return from the client - should be set (via * the -jwsacc command-line argument value) only for * command-line clients; otherwise it can prematurely end the JVM when * the GUI and other user work is continuing */ private static boolean exitAfterReturn = false; /* *Normally the ACC is not run with the Java Web Start classloader as the *parent class loader because this causes problems loading dynamic stubs. *To profile performance, though, sometimes we need to keep the JWS *class loader as the parent rather than skipping it. */ private static boolean keepJWSClassLoader = false; private static boolean runOnSwingThread = false; /** helper for building the class loader and policy changes */ private static ClassPathManager classPathManager = null; /** URLs for downloaded JAR files to be used in the class path */ private static URL [] downloadedJarURLs; /** URLs for persistence-related JAR files for the class path and permissions */ private static URL [] persistenceJarURLs; /** localizable strings */ private static final ResourceBundle rb = ResourceBundle.getBundle( dotToSlash(JWSACCMain.class.getPackage().getName() + ".LocalStrings")); /** make the arguments passed to the constructor available to the main method */ private String args[]; /** Creates a new instance of JWSMain */ public JWSACCMain(String[] args) { this.args = args; } /** * @param args the command line arguments */ public static void main(String[] args) { try { args = prepareJWSArgs(args); try { classPathManager = getClassPathManager(); downloadedJarURLs = classPathManager.locateDownloadedJars(); persistenceJarURLs = classPathManager.locatePersistenceJARs(); } catch (Throwable thr) { throw new IllegalArgumentException(rb.getString("jwsacc.errorLocJARs"), thr); } /* *Before creating the new instance of the real ACC main, set permissions *so ACC and the user's app client can function properly. */ setPermissions(); /* *Make sure that the main ACC class is instantiated and run in the *same thread. Java Web Start may not normally do so. */ JWSACCMain jwsACCMain = new JWSACCMain(args); if (runOnSwingThread) { SwingUtilities.invokeAndWait(jwsACCMain); } else { jwsACCMain.run(); } /* *Note that the app client is responsible for closing all GUI *components or the JVM will never exit. */ } catch (Throwable thr) { System.exit(1); } } private static String dotToSlash(String orig) { return orig.replaceAll("\\.","/"); } public void run() { // Main.main(args); int exitValue = 0; try { File downloadedAppclientJarFile = findAppClientFileForJWSLaunch(getClass().getClassLoader()); ClassLoader loader = prepareClassLoader(downloadedAppclientJarFile); /* *Set a property that the ACC will retrieve during a JWS launch *to locate the app client jar file. */ System.setProperty("com.sun.aas.downloaded.appclient.jar", downloadedAppclientJarFile.getAbsolutePath()); Thread.currentThread().setContextClassLoader(loader); /* *Use the prepared class loader to load the ACC main method, prepare *the arguments to the constructor, and invoke the static main method. */ Constructor constr = null; Class mainClass = Class.forName("com.sun.enterprise.appclient.MainWithModuleSupport", true /* initialize */, loader); constr = mainClass.getConstructor( new Class[] { String[].class, URL[].class } ); constr.newInstance(args, persistenceJarURLs); } catch(Throwable thr) { exitValue = 1; /* *Display the throwable and stack trace to System.err, then *display it to the user using the GUI dialog box. */ System.err.println(rb.getString("jwsacc.errorLaunch")); System.err.println(thr.toString()); thr.printStackTrace(); ErrorDisplayDialog.showErrors(thr, rb); } finally { /* *If the user has requested, invoke System.exit as soon as the main *method returns. Do so on the Swing event thread so the ACC *main can complete whatever it may be doing. */ if (exitAfterReturn || (exitValue != 0)) { Runnable exit = new Runnable() { private int statusValue; public void run() { System.out.printf("Exiting after return from client with status %1$d%n", statusValue); System.exit(statusValue); } public Runnable init(int exitStatus) { statusValue = exitStatus; return this; } }.init(exitValue); if (runOnSwingThread) { SwingUtilities.invokeLater(exit); } else { exit.run(); } } } } /** *Process any command line arguments that are targeted for the *Java Web Start ACC main program (this class) as opposed to the *regular ACC or the client itself. *@param args the original command line arguments *@return command arguments with any handled by JWS ACC removed */ private static String[] prepareJWSArgs(String[] args) { Vector<String> JWSACCArgs = new Vector Other Glassfish examples (source code examples)Here is a short list of links related to this Glassfish JWSACCMain.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.