alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Glassfish example source code file (SecurityServicesUtil.java)

This example Glassfish source code file (SecurityServicesUtil.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Glassfish tags/keywords

auditmanager, callbackhandler, callbackhandler, habitat, inject, inject, localstringmanagerimpl, log, logger, logging, processenvironment, processenvironment, securityservicesutil, securityservicesutil, serverenvironment, service

The Glassfish SecurityServicesUtil.java source code

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2008-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.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sun.enterprise.security;

import com.sun.enterprise.security.audit.AuditManager;
import com.sun.enterprise.util.LocalStringManagerImpl;
import org.glassfish.api.admin.ServerEnvironment;
import com.sun.logging.LogDomains;
import java.util.logging.Logger;
import javax.security.auth.callback.CallbackHandler;
import org.jvnet.hk2.component.Habitat;
import org.jvnet.hk2.annotations.Service;
import org.jvnet.hk2.annotations.Inject;
import org.jvnet.hk2.component.Singleton;
import org.jvnet.hk2.annotations.Scoped;
import org.glassfish.api.admin.ProcessEnvironment;
import org.glassfish.api.admin.ProcessEnvironment.ProcessType;

@Service
@Scoped(Singleton.class)
public class SecurityServicesUtil {

    //private static Habitat habitat = Globals.getDefaultHabitat();
    @Inject
    private static Habitat habitat;
    
    private static final LocalStringManagerImpl _localStrings =
            new LocalStringManagerImpl(SecurityServicesUtil.class);
    private static final Logger _logger = LogDomains.getLogger(SecurityServicesUtil.class, LogDomains.SECURITY_LOGGER);
    // Using SharedSecureRandom from internal-api instead.
    // SecureRandom number used for HTTPS and IIOP/SSL.
    // This number is accessed by iiop/IIOPSSLSocketFactory
    // & web/security/SSLSocketFactory classes.
    //public static final SecureRandom secureRandom = new SecureRandom();

    @Inject
    private ProcessEnvironment processEnv;
    
    @Inject
    private ServerEnvironment env;

    @Inject 
    private AuditManager auditManager;
    
//    static {
//        secureRandom.setSeed(System.currentTimeMillis());
//    }

    //the appclient CBH
    private CallbackHandler callbackHandler;
    
    /**
     * replacing all of this with SharedSecureRandom
     * So on restart, all your seed material comes from a new time that varies
     * little from something guessable, plus something you repeat every run?
     * Seems weak to me.  Why do you want to keep the seed around?
     * There are some situations where this might be needed,
     * but if you're just looking for as random numbers as possible,
     * I don't think this is not a good way to achieve it.
     * code moved from J2EEServer.run()
     */
//    public void initSecureSeed() {
//
//        File secureSeedFile = null;
//        if (Util.isEmbeddedServer()) {
//            try {
//                secureSeedFile = Util.writeConfigFileToTempDir("secure.seed");
//
//            } catch (IOException ex) {
//                String errmsg =
//                        "IOException while constructing embedded config file";
//                _logger.log(Level.WARNING, errmsg);
//            }
//        } else {
//            secureSeedFile = new File(env.getConfigDirPath(), "secure.seed");
//        }
//
//        // read the secure random from the file
//        long seed = readSecureSeed(secureSeedFile);
//        secureRandom.setSeed(seed);
//        // generate a new one for the next startup
//        seed = secureRandom.nextLong();
//        writeSecureSeed(secureSeedFile, seed);
//        secureSeedFile = null;
//    }

    /** read the secure random number from the file.
     *  If the seed is not present, the default expensive SecureRandom seed
     *  generation algorithm is invoked to return a new seed number
     *  @param fname the file to be read - here secure.seed file.
     */
//    private long readSecureSeed(File fname) {
//        byte[] seed;
//        try {
//            BufferedReader fis = new BufferedReader(new FileReader(fname));
//            try {
//                String line = fis.readLine();
//                fis.close();
//                // returning a long value.
//                Long lseed = new Long(line);
//                return lseed.longValue();
//            } catch (IOException e) {
//                if (fis != null) {
//                    fis.close();
//                }
//            }
//        } catch (Throwable e) {  // IASRI 4666401 if all fails just create new
//        }
//        // BEGIN IASRI 4703002
//        // In order to work around JVM bug 4709460 avoid internal seeding.
//        // (Call setSeed again (see static block) to attempt to add some
//        // minimal randomness; setSeed calls are cumulative)
//
//        secureRandom.setSeed(System.currentTimeMillis());
//        long newSeed = secureRandom.nextLong();
//        return newSeed;
//    }

    /** write the new secure seed to the secure.seed file to speed up
     * startup the next time the server is started.
     * @param fname secure.seed file
     * @param seed seed the value of the 8 byte seed.
     */
//    private void writeSecureSeed(File fname, long seed) {
//        try {
//            FileOutputStream fos = new FileOutputStream(fname);
//            String sseed = Long.toString(seed);
//            fos.write(sseed.getBytes());
//            fos.close();
//        } catch (IOException e) {
//            String errmsg =
//                    _localStrings.getLocalString("j2ee.startupslow",
//                    "Cannot write the seed file for fast startup. The next startup will be slow.");
//
//            _logger.log(Level.WARNING, errmsg);
//        }
//
//    }

    public Habitat getHabitat() {
        return habitat;
    }
    
    public AuditManager getAuditManager() {
        return auditManager;
    }

    public static SecurityServicesUtil getInstance() {
        // return my singleton service
        if (habitat == null) {
            return null;
        }
        return habitat.getComponent(SecurityServicesUtil.class);
    }

    public ProcessEnvironment getProcessEnv() {
        return processEnv;
    }
    
    public boolean isACC() {
        return processEnv.getProcessType().equals(ProcessType.ACC);
    }
    public boolean isServer() {
        return processEnv.getProcessType().isServer();
    }
    public boolean isNotServerOrACC() {
        return processEnv.getProcessType().equals(ProcessType.Other);
    }

    public CallbackHandler getCallbackHandler() {
        return callbackHandler;
    }

    public void setCallbackHandler(CallbackHandler callbackHandler) {
        this.callbackHandler = callbackHandler;
    }
    
}

Other Glassfish examples (source code examples)

Here is a short list of links related to this Glassfish SecurityServicesUtil.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

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.