Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Properties;

import org.hibernate.boot.cfgxml.spi.LoadedConfig;
Expand Down Expand Up @@ -48,27 +50,34 @@ public ConfigLoader(BootstrapServiceRegistry bootstrapServiceRegistry) {
}

public LoadedConfig loadConfigXmlResource(String cfgXmlResourceName) {
final InputStream stream = bootstrapServiceRegistry.getService( ClassLoaderService.class ).locateResourceStream( cfgXmlResourceName );
if ( stream == null ) {
throw new ConfigurationException( "Could not locate cfg.xml resource [" + cfgXmlResourceName + "]" );
}

try {
final JaxbCfgHibernateConfiguration jaxbCfg = jaxbProcessorHolder.getValue().unmarshal(
stream,
new Origin( SourceType.RESOURCE, cfgXmlResourceName )
);
final PrivilegedAction<JaxbCfgHibernateConfiguration> action = new PrivilegedAction<JaxbCfgHibernateConfiguration>() {
@Override
public JaxbCfgHibernateConfiguration run() {
final InputStream stream = bootstrapServiceRegistry.getService( ClassLoaderService.class ).locateResourceStream( cfgXmlResourceName );
if ( stream == null ) {
throw new ConfigurationException( "Could not locate cfg.xml resource [" + cfgXmlResourceName + "]" );
}

return LoadedConfig.consume( jaxbCfg );
}
finally {
try {
stream.close();
}
catch (IOException e) {
log.debug( "Unable to close cfg.xml resource stream", e );
try {
return jaxbProcessorHolder.getValue().unmarshal(
stream,
new Origin( SourceType.RESOURCE, cfgXmlResourceName )
);
}
finally {
try {
stream.close();
}
catch ( IOException e ) {
log.debug( "Unable to close cfg.xml resource stream", e );
}
}
}
}
};

return LoadedConfig.consume(
System.getSecurityManager() != null ? AccessController.doPrivileged( action ) : action.run()
);
}

public LoadedConfig loadConfigXmlFile(File cfgXmlFile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
package org.hibernate.boot.jaxb.internal;

import java.io.InputStream;
import java.security.AccessController;
import java.security.PrivilegedAction;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
Expand Down Expand Up @@ -98,8 +101,15 @@ protected XMLEventReader createReader(Source source, Origin origin) {

private Binding doBind(XMLEventReader eventReader, Origin origin) {
try {
final StartElement rootElementStartEvent = seekRootElementStartEvent( eventReader, origin );
return doBind( eventReader, rootElementStartEvent, origin );
final PrivilegedAction<Binding> action = new PrivilegedAction<Binding>() {
@Override
public Binding run() {
final StartElement rootElementStartEvent = seekRootElementStartEvent( eventReader, origin );
return doBind( eventReader, rootElementStartEvent, origin );
}
};

return System.getSecurityManager() != null ? AccessController.doPrivileged( action ) : action.run();
}
finally {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,16 @@ public ClassLoaderServiceImpl(Collection<ClassLoader> providedClassLoaders, Tccl
orderedClassLoaderSet.add( ClassLoaderServiceImpl.class.getClassLoader() );

// now build the aggregated class loader...
this.aggregatedClassLoader = AccessController.doPrivileged( new PrivilegedAction<AggregatedClassLoader>() {
final PrivilegedAction<AggregatedClassLoader> action = new PrivilegedAction<AggregatedClassLoader>() {
@Override
public AggregatedClassLoader run() {
return new AggregatedClassLoader( orderedClassLoaderSet, lookupPrecedence );
}
} );
};

this.aggregatedClassLoader = System.getSecurityManager() != null
? AccessController.doPrivileged( action )
: action.run();
}

/**
Expand Down Expand Up @@ -347,49 +352,62 @@ protected Class<?> findClass(String name) throws ClassNotFoundException {
@Override
@SuppressWarnings({"unchecked"})
public <T> Class<T> classForName(String className) {
try {
return (Class<T>) Class.forName( className, true, getAggregatedClassLoader() );
}
catch (Exception e) {
throw new ClassLoadingException( "Unable to load class [" + className + "]", e );
}
catch (LinkageError e) {
throw new ClassLoadingException( "Unable to load class [" + className + "]", e );
}
final PrivilegedAction<Class<T>> action = new PrivilegedAction<Class<T>>() {
@Override
public Class<T> run() {
try {
return (Class<T>) Class.forName( className, true, getAggregatedClassLoader() );
}
catch (Exception e) {
throw new ClassLoadingException( "Unable to load class [" + className + "]", e );
}
catch (LinkageError e) {
throw new ClassLoadingException( "Unable to load class [" + className + "]", e );
}
}
};

return System.getSecurityManager() != null ? AccessController.doPrivileged( action ) : action.run();
}

@Override
public URL locateResource(String name) {
// first we try name as a URL
try {
return new URL( name );
}
catch (Exception ignore) {
}
public URL locateResource(final String name) {
final PrivilegedAction<URL> action = new PrivilegedAction<URL>() {
@Override
public URL run() {
try {
return new URL( name );
}
catch (Exception ignore) {
}

try {
final URL url = getAggregatedClassLoader().getResource( name );
if ( url != null ) {
return url;
}
}
catch (Exception ignore) {
}
try {
final URL url = getAggregatedClassLoader().getResource( name );
if ( url != null ) {
return url;
}
}
catch (Exception ignore) {
}

if ( name.startsWith( "/" ) ) {
name = name.substring( 1 );
if ( name.startsWith( "/" ) ) {
final String resourceName = name.substring( 1 );

try {
final URL url = getAggregatedClassLoader().getResource( name );
if ( url != null ) {
return url;
try {
final URL url = getAggregatedClassLoader().getResource( resourceName );
if ( url != null ) {
return url;
}
}
catch (Exception ignore) {
}
}

return null;
}
catch (Exception ignore) {
}
}
};

return null;
return System.getSecurityManager() != null ? AccessController.doPrivileged( action ) : action.run();
}

@Override
Expand Down Expand Up @@ -456,16 +474,22 @@ public List<URL> locateResources(String name) {
@Override
@SuppressWarnings("unchecked")
public <S> Collection<S> loadJavaServices(Class<S> serviceContract) {
ServiceLoader<S> serviceLoader = serviceLoaders.get( serviceContract );
if ( serviceLoader == null ) {
serviceLoader = ServiceLoader.load( serviceContract, getAggregatedClassLoader() );
serviceLoaders.put( serviceContract, serviceLoader );
}
final LinkedHashSet<S> services = new LinkedHashSet<S>();
for ( S service : serviceLoader ) {
services.add( service );
}
return services;
final PrivilegedAction<Collection<S>> action = new PrivilegedAction<Collection<S>>() {
@Override
public Collection<S> run() {
ServiceLoader<S> serviceLoader = serviceLoaders.get( serviceContract );
if ( serviceLoader == null ) {
serviceLoader = ServiceLoader.load( serviceContract, getAggregatedClassLoader() );
serviceLoaders.put( serviceContract, serviceLoader );
}
final LinkedHashSet<S> services = new LinkedHashSet<S>();
for ( S service : serviceLoader ) {
services.add( service );
}
return services;
}
};
return System.getSecurityManager() != null ? AccessController.doPrivileged( action ) : action.run();
}

@Override
Expand All @@ -480,7 +504,13 @@ public <T> T generateProxy(InvocationHandler handler, Class... interfaces) {

@Override
public <T> T workWithClassLoader(Work<T> work) {
return work.doWork( getAggregatedClassLoader() );
final PrivilegedAction<T> action = new PrivilegedAction<T>() {
@Override
public T run() {
return work.doWork( getAggregatedClassLoader() );
}
};
return System.getSecurityManager() != null ? AccessController.doPrivileged( action ) : action.run();
}

private ClassLoader getAggregatedClassLoader() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;

import org.hibernate.HibernateException;
import org.hibernate.cfg.Environment;
Expand Down Expand Up @@ -113,28 +115,33 @@ private ConfigHelper() {
}

public static InputStream getResourceAsStream(String resource) {
String stripped = resource.startsWith( "/" )
? resource.substring( 1 )
: resource;

InputStream stream = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if ( classLoader != null ) {
stream = classLoader.getResourceAsStream( stripped );
}
if ( stream == null ) {
stream = Environment.class.getResourceAsStream( resource );
}
if ( stream == null ) {
stream = Environment.class.getClassLoader().getResourceAsStream( stripped );
}
if ( stream == null ) {
throw new HibernateException( resource + " not found" );
}
return stream;
final PrivilegedAction<InputStream> action = new PrivilegedAction<InputStream>() {
@Override
public InputStream run() {
String stripped = resource.startsWith( "/" )
? resource.substring( 1 )
: resource;

InputStream stream = null;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if ( classLoader != null ) {
stream = classLoader.getResourceAsStream( stripped );
}
if ( stream == null ) {
stream = Environment.class.getResourceAsStream( resource );
}
if ( stream == null ) {
stream = Environment.class.getClassLoader().getResourceAsStream( stripped );
}
if ( stream == null ) {
throw new HibernateException( resource + " not found" );
}
return stream;
}
};
return System.getSecurityManager() != null ? AccessController.doPrivileged( action ) : action.run();
}


public static InputStream getUserResourceAsStream(String resource) {
boolean hasLeadingSlash = resource.startsWith( "/" );
String stripped = hasLeadingSlash ? resource.substring( 1 ) : resource;
Expand Down
Loading