Skip to content

Commit

Permalink
Issue #2262 Sanitize osgi code
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Bartel <janb@webtide.com>
  • Loading branch information
janbartel committed Mar 1, 2018
1 parent 1173be3 commit 05e2527
Show file tree
Hide file tree
Showing 20 changed files with 83 additions and 81 deletions.
Expand Up @@ -25,7 +25,6 @@

import javax.servlet.ServletContainerInitializer;


import org.eclipse.jetty.annotations.AnnotationParser.Handler;
import org.eclipse.jetty.osgi.boot.OSGiWebInfConfiguration;
import org.eclipse.jetty.osgi.boot.OSGiWebappConstants;
Expand Down Expand Up @@ -116,6 +115,7 @@ public void parseWebInfLib (WebAppContext context, org.eclipse.jetty.annotations
_webInfLibStats = new CounterStatistic();

Bundle webbundle = (Bundle) context.getAttribute(OSGiWebappConstants.JETTY_OSGI_BUNDLE);
@SuppressWarnings("unchecked")
Set<Bundle> fragAndRequiredBundles = (Set<Bundle>)context.getAttribute(OSGiWebInfConfiguration.FRAGMENT_AND_REQUIRED_BUNDLES);
if (fragAndRequiredBundles != null)
{
Expand Down Expand Up @@ -229,7 +229,7 @@ protected void parseBundle(WebAppContext context, AnnotationParser parser,
{

Resource bundleRes = parser.getResource(bundle);
Set<Handler> handlers = new HashSet<Handler>();
Set<Handler> handlers = new HashSet<>();
handlers.addAll(_discoverableAnnotationHandlers);
if (_classInheritanceHandler != null)
handlers.add(_classInheritanceHandler);
Expand Down
Expand Up @@ -42,10 +42,10 @@ public class AnnotationParser extends org.eclipse.jetty.annotations.AnnotationPa
{
private Set<URI> _alreadyParsed = ConcurrentHashMap.newKeySet();

private ConcurrentHashMap<URI,Bundle> _uriToBundle = new ConcurrentHashMap<URI, Bundle>();
private ConcurrentHashMap<Bundle,Resource> _bundleToResource = new ConcurrentHashMap<Bundle,Resource>();
private ConcurrentHashMap<Resource, Bundle> _resourceToBundle = new ConcurrentHashMap<Resource, Bundle>();
private ConcurrentHashMap<Bundle,URI> _bundleToUri = new ConcurrentHashMap<Bundle, URI>();
private ConcurrentHashMap<URI,Bundle> _uriToBundle = new ConcurrentHashMap<>();
private ConcurrentHashMap<Bundle,Resource> _bundleToResource = new ConcurrentHashMap<>();
private ConcurrentHashMap<Resource, Bundle> _resourceToBundle = new ConcurrentHashMap<>();
private ConcurrentHashMap<Bundle,URI> _bundleToUri = new ConcurrentHashMap<>();

public AnnotationParser(int javaPlatform)
{
Expand Down Expand Up @@ -126,7 +126,7 @@ protected void parse(Set<? extends Handler> handlers, Bundle bundle)
bundleClasspath = ".";
}
//order the paths first by the number of tokens in the path second alphabetically.
TreeSet<String> paths = new TreeSet<String>(
TreeSet<String> paths = new TreeSet<>(
new Comparator<String>()
{
public int compare(String o1, String o2)
Expand Down Expand Up @@ -176,6 +176,7 @@ else if (bundle.getEntry("/target/classes/") != null)
paths.add("/target/classes/");
}
}
@SuppressWarnings("rawtypes")
Enumeration classes = bundle.findEntries("/","*.class",true);
if (classes == null)
{
Expand Down
Expand Up @@ -48,7 +48,7 @@ public class BundleWebAppProvider extends AbstractWebAppProvider implements Bund
/**
* Map of Bundle to App. Used when a Bundle contains a webapp.
*/
private Map<Bundle, App> _bundleMap = new HashMap<Bundle, App>();
private Map<Bundle, App> _bundleMap = new HashMap<>();

private ServiceRegistration _serviceRegForBundles;

Expand Down Expand Up @@ -124,7 +124,7 @@ protected void doStart() throws Exception
_webappTracker = new WebAppTracker(FrameworkUtil.getBundle(this.getClass()).getBundleContext(), getServerInstanceWrapper().getManagedServerName());
_webappTracker.open();
//register as an osgi service for deploying bundles, advertising the name of the jetty Server instance we are related to
Dictionary<String,String> properties = new Hashtable<String,String>();
Dictionary<String,String> properties = new Hashtable<>();
properties.put(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME, getServerInstanceWrapper().getManagedServerName());
_serviceRegForBundles = FrameworkUtil.getBundle(this.getClass()).getBundleContext().registerService(BundleProvider.class.getName(), this, properties);
super.doStart();
Expand Down Expand Up @@ -175,6 +175,7 @@ public boolean bundleAdded (Bundle bundle) throws Exception
String contextPath = null;
try
{
@SuppressWarnings("unchecked")
Dictionary<String,String> headers = bundle.getHeaders();

//does the bundle have a OSGiWebappConstants.JETTY_WAR_FOLDER_PATH
Expand Down
Expand Up @@ -79,7 +79,7 @@ public void start(final BundleContext context) throws Exception
_jettyServerServiceTracker.open();

// Create a default jetty instance right now.
Server defaultServer = DefaultJettyAtJettyHomeHelper.startJettyAtJettyHome(context);
DefaultJettyAtJettyHomeHelper.startJettyAtJettyHome(context);
}


Expand Down
Expand Up @@ -95,15 +95,15 @@ public void preConfigure(final WebAppContext context) throws Exception
// 2. DeployerManager.setContextAttribute CONTAINER_BUNDLE_PATTERN
String tmp = (String)context.getAttribute(CONTAINER_BUNDLE_PATTERN);
Pattern pattern = (tmp==null?null:Pattern.compile(tmp));
List<String> names = new ArrayList<String>();
List<String> names = new ArrayList<>();
tmp = System.getProperty(SYS_PROP_TLD_BUNDLES);
if (tmp != null)
{
StringTokenizer tokenizer = new StringTokenizer(tmp, ", \n\r\t", false);
while (tokenizer.hasMoreTokens())
names.add(tokenizer.nextToken());
}
HashSet<Resource> matchingResources = new HashSet<Resource>();
HashSet<Resource> matchingResources = new HashSet<>();
if ( !names.isEmpty() || pattern != null)
{
Bundle[] bundles = FrameworkUtil.getBundle(OSGiWebInfConfiguration.class).getBundleContext().getBundles();
Expand Down Expand Up @@ -153,7 +153,7 @@ public void postConfigure(WebAppContext context) throws Exception
protected List<Resource> findJars (WebAppContext context)
throws Exception
{
List<Resource> mergedResources = new ArrayList<Resource>();
List<Resource> mergedResources = new ArrayList<>();
//get jars from WEB-INF/lib if there are any
List<Resource> webInfJars = super.findJars(context);
if (webInfJars != null)
Expand All @@ -163,17 +163,19 @@ protected List<Resource> findJars (WebAppContext context)
Bundle[] bundles = PackageAdminServiceTracker.INSTANCE.getFragmentsAndRequiredBundles((Bundle)context.getAttribute(OSGiWebappConstants.JETTY_OSGI_BUNDLE));
if (bundles != null && bundles.length > 0)
{
@SuppressWarnings("unchecked")
Set<Bundle> fragsAndReqsBundles = (Set<Bundle>)context.getAttribute(FRAGMENT_AND_REQUIRED_BUNDLES);
if (fragsAndReqsBundles == null)
{
fragsAndReqsBundles = new HashSet<Bundle>();
fragsAndReqsBundles = new HashSet<>();
context.setAttribute(FRAGMENT_AND_REQUIRED_BUNDLES, fragsAndReqsBundles);
}

@SuppressWarnings("unchecked")
Set<Resource> fragsAndReqsResources = (Set<Resource>)context.getAttribute(FRAGMENT_AND_REQUIRED_RESOURCES);
if (fragsAndReqsResources == null)
{
fragsAndReqsResources = new HashSet<Resource>();
fragsAndReqsResources = new HashSet<>();
context.setAttribute(FRAGMENT_AND_REQUIRED_RESOURCES, fragsAndReqsResources);
}

Expand Down Expand Up @@ -207,12 +209,13 @@ protected List<Resource> findJars (WebAppContext context)
@Override
public void configure(WebAppContext context) throws Exception
{
TreeMap<String, Resource> prependedResourcesPath = new TreeMap<String, Resource>();
TreeMap<String, Resource> appendedResourcesPath = new TreeMap<String, Resource>();
TreeMap<String, Resource> prependedResourcesPath = new TreeMap<>();
TreeMap<String, Resource> appendedResourcesPath = new TreeMap<>();

Bundle bundle = (Bundle)context.getAttribute(OSGiWebappConstants.JETTY_OSGI_BUNDLE);
if (bundle != null)
{
@SuppressWarnings("unchecked")
Set<Bundle> fragments = (Set<Bundle>)context.getAttribute(FRAGMENT_AND_REQUIRED_BUNDLES);
if (fragments != null && !fragments.isEmpty())
{
Expand All @@ -238,8 +241,9 @@ public void configure(WebAppContext context) throws Exception
}
if (!appendedResourcesPath.isEmpty())
{
LinkedHashSet<Resource> resources = new LinkedHashSet<Resource>();
LinkedHashSet<Resource> resources = new LinkedHashSet<>();
//Add in any existing setting of extra resource dirs
@SuppressWarnings("unchecked")
Set<Resource> resourceDirs = (Set<Resource>)context.getAttribute(WebInfConfiguration.RESOURCE_DIRS);
if (resourceDirs != null && !resourceDirs.isEmpty())
resources.addAll(resourceDirs);
Expand Down Expand Up @@ -272,7 +276,7 @@ public void configure(WebAppContext context) throws Exception
private List<Resource> getBundleAsResource(Bundle bundle)
throws Exception
{
List<Resource> resources = new ArrayList<Resource>();
List<Resource> resources = new ArrayList<>();

File file = BundleFileLocatorHelperFactory.getFactory().getHelper().getBundleInstallLocation(bundle);
if (file.isDirectory())
Expand Down
Expand Up @@ -29,7 +29,6 @@
import org.eclipse.jetty.osgi.boot.internal.serverfactory.ServerInstanceWrapper;
import org.eclipse.jetty.osgi.boot.utils.Util;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.osgi.framework.Bundle;
Expand All @@ -52,7 +51,7 @@ public class ServiceContextProvider extends AbstractContextProvider implements S
{
private static final Logger LOG = Log.getLogger(AbstractContextProvider.class);

private Map<ServiceReference, App> _serviceMap = new HashMap<ServiceReference, App>();
private Map<ServiceReference, App> _serviceMap = new HashMap<>();

private ServiceRegistration _serviceRegForServices;

Expand Down Expand Up @@ -168,7 +167,7 @@ public boolean serviceAdded (ServiceReference serviceRef, ContextHandler context
contextFile = (String)serviceRef.getProperty(OSGiWebappConstants.SERVICE_PROP_CONTEXT_FILE_PATH);

String[] keys = serviceRef.getPropertyKeys();
Dictionary properties = new Hashtable<String, Object>();
Dictionary<String,Object> properties = new Hashtable<>();
if (keys != null)
{
for (String key:keys)
Expand Down Expand Up @@ -226,7 +225,7 @@ protected void doStart() throws Exception


//register as an osgi service for deploying contexts defined in a bundle, advertising the name of the jetty Server instance we are related to
Dictionary<String,String> properties = new Hashtable<String,String>();
Dictionary<String,String> properties = new Hashtable<>();
properties.put(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME, getServerInstanceWrapper().getManagedServerName());

//register as an osgi service for deploying contexts, advertising the name of the jetty Server instance we are related to
Expand Down
Expand Up @@ -29,7 +29,6 @@
import org.eclipse.jetty.osgi.boot.internal.serverfactory.ServerInstanceWrapper;
import org.eclipse.jetty.osgi.boot.utils.Util;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.webapp.WebAppContext;
Expand All @@ -53,7 +52,7 @@ public class ServiceWebAppProvider extends AbstractWebAppProvider implements Ser
/**
* Map of ServiceRef to App. Used when it is an osgi service that is a WebAppContext.
*/
private Map<ServiceReference, App> _serviceMap = new HashMap<ServiceReference, App>();
private Map<ServiceReference, App> _serviceMap = new HashMap<>();

private ServiceRegistration _serviceRegForServices;

Expand Down Expand Up @@ -117,7 +116,7 @@ public void removedService(ServiceReference reference, Object service)
public class ServiceApp extends OSGiApp
{

public ServiceApp(DeploymentManager manager, AppProvider provider, Bundle bundle, Dictionary properties, String originId)
public ServiceApp(DeploymentManager manager, AppProvider provider, Bundle bundle, Dictionary<String,String> properties, String originId)
{
super(manager, provider, bundle, properties, originId);
}
Expand Down Expand Up @@ -167,7 +166,7 @@ public boolean serviceAdded (ServiceReference serviceRef, ContextHandler context


WebAppContext webApp = (WebAppContext)context;
Dictionary properties = new Hashtable<String,String>();
Dictionary<String,String> properties = new Hashtable<>();

String contextPath = (String)serviceRef.getProperty(OSGiWebappConstants.RFC66_WEB_CONTEXTPATH);
if (contextPath == null)
Expand Down Expand Up @@ -272,7 +271,7 @@ protected void doStart() throws Exception
webappTracker.open();

//register as an osgi service for deploying bundles, advertising the name of the jetty Server instance we are related to
Dictionary<String,String> properties = new Hashtable<String,String>();
Dictionary<String,String> properties = new Hashtable<>();
properties.put(OSGiServerConstants.MANAGED_JETTY_SERVER_NAME, getServerInstanceWrapper().getManagedServerName());

//register as an osgi service for deploying contexts (discovered as osgi services), advertising the name of the jetty Server instance we are related to
Expand Down
Expand Up @@ -102,7 +102,7 @@ public static Server startJettyAtJettyHome(BundleContext bundleContext) throws E
File jettyHomeDir = null;
Bundle jettyHomeBundle = null;

Dictionary<String,String> properties = new Hashtable<String,String>();
Dictionary<String, Object> properties = new Hashtable<>();
if (jettyHomeSysProp != null)
{
jettyHomeSysProp = Util.resolvePropertyValue(jettyHomeSysProp);
Expand Down Expand Up @@ -157,8 +157,8 @@ else if (jettyHomeBundleSysProp != null)
List<URL> configURLs = jettyHomeDir != null ? getJettyConfigurationURLs(jettyHomeDir) : getJettyConfigurationURLs(jettyHomeBundle, properties);

LOG.info("Configuring the default jetty server with {}",configURLs);
String home=properties.get(OSGiServerConstants.JETTY_HOME);
String base=properties.get(OSGiServerConstants.JETTY_BASE);
String home=(String)properties.get(OSGiServerConstants.JETTY_HOME);
String base=(String)properties.get(OSGiServerConstants.JETTY_BASE);
if (base==null)
base=home;
LOG.info("JETTY.HOME="+home);
Expand All @@ -178,7 +178,6 @@ else if (jettyHomeBundleSysProp != null)
Util.setProperty(properties, OSGiServerConstants.JETTY_BASE, base);
Server server = ServerInstanceWrapper.configure(null, configURLs, properties);


//Register the default Server instance as an OSGi service.
//The JettyServerServiceTracker will notice it and set it up to deploy bundles as wars etc
bundleContext.registerService(Server.class.getName(), server, properties);
Expand Down Expand Up @@ -213,7 +212,7 @@ else if (jettyHomeBundleSysProp != null)
private static List<URL> getJettyConfigurationURLs(File jettyhome)
throws MalformedURLException
{
List<URL> configURLs = new ArrayList<URL>();
List<URL> configURLs = new ArrayList<>();
String jettyetc = System.getProperty(JETTY_ETC_FILES, DEFAULT_JETTY_ETC_FILES);
StringTokenizer tokenizer = new StringTokenizer(jettyetc, ";,", false);
while (tokenizer.hasMoreTokens())
Expand Down Expand Up @@ -241,7 +240,7 @@ private static List<URL> getJettyConfigurationURLs(File jettyhome)
private static List<URL> getJettyConfigurationURLs(Bundle configurationBundle, Dictionary properties)
throws Exception
{
List<URL> configURLs = new ArrayList<URL>();
List<URL> configURLs = new ArrayList<>();
String files = System.getProperty(JETTY_ETC_FILES, DEFAULT_JETTY_ETC_FILES);
StringTokenizer tokenizer = new StringTokenizer(files, ";,", false);

Expand Down
Expand Up @@ -18,6 +18,8 @@

package org.eclipse.jetty.osgi.boot.internal.serverfactory;

import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Properties;

import org.eclipse.jetty.osgi.boot.OSGiServerConstants;
Expand Down Expand Up @@ -52,11 +54,10 @@ public Object addingService(ServiceReference sr)
if (name == null) { throw new IllegalArgumentException("The property " + OSGiServerConstants.MANAGED_JETTY_SERVER_NAME + " is mandatory"); }
if (LOG.isDebugEnabled()) LOG.debug("Adding Server {}", name);
ServerInstanceWrapper wrapper = new ServerInstanceWrapper(name);
Properties props = new Properties();
Dictionary<String,Object> props = new Hashtable<>();
for (String key : sr.getPropertyKeys())
{
Object value = sr.getProperty(key);
props.put(key, value);
props.put(key, sr.getProperty(key));
}
try
{
Expand Down

0 comments on commit 05e2527

Please sign in to comment.