Create a BundleDefinition based on the properties file.

@param bundleName
the bundle name
@return the bundleDefinition
185867
private ResourceBundleDefinition buildCustomBundleDefinition(String bundleName, boolean isChildBundle) {

		// Id for the bundle
		String bundleId = props.getCustomBundleProperty(bundleName, BUNDLE_FACTORY_CUSTOM_ID);
		if (null == bundleId && !isChildBundle)
			throw new IllegalArgumentException(
					"No id defined for the bundle with name:" + bundleName + ". Please specify one in configuration. ");

		// Whether it's a composite or not
		boolean isComposite = Boolean
				.parseBoolean(props.getCustomBundleProperty(bundleName, BUNDLE_FACTORY_CUSTOM_COMPOSITE_FLAG, "false"));

		// Create definition and set its id
		ResourceBundleDefinition bundle = new ResourceBundleDefinition();
		bundle.setBundleId(bundleId);
		bundle.setBundleName(bundleName);
		bundle.setBundlePrefix(props.getCustomBundleProperty(bundleName, BUNDLE_FACTORY_CUSTOM_BUNDLE_PREFIX));

		// Whether it's global or not
		boolean isGlobal = Boolean
				.parseBoolean(props.getCustomBundleProperty(bundleName, BUNDLE_FACTORY_CUSTOM_GLOBAL_FLAG, "false"));
		bundle.setGlobal(isGlobal);

		// Set order if its a global bundle
		if (isGlobal) {
			int order = Integer.parseInt(props.getCustomBundleProperty(bundleName, BUNDLE_FACTORY_CUSTOM_ORDER, "0"));
			bundle.setInclusionOrder(order);
		}

		// Override bundle postprocessor
		if (null != props.getCustomBundleProperty(bundleName, BUNDLE_FACTORY_CUSTOM_POSTPROCESSOR))
			bundle.setBundlePostProcessorKeys(
					props.getCustomBundleProperty(bundleName, BUNDLE_FACTORY_CUSTOM_POSTPROCESSOR));

		// Override unitary postprocessor
		if (null != props.getCustomBundleProperty(bundleName, BUNDLE_FACTORY_CUSTOM_FILE_POSTPROCESSOR))
			bundle.setUnitaryPostProcessorKeys(
					props.getCustomBundleProperty(bundleName, BUNDLE_FACTORY_CUSTOM_FILE_POSTPROCESSOR));

		// Use only with debug mode on
		boolean isDebugOnly = Boolean
				.parseBoolean(props.getCustomBundleProperty(bundleName, BUNDLE_FACTORY_CUSTOM_DEBUGONLY, "false"));
		bundle.setDebugOnly(isDebugOnly);

		// Use only with debug mode off
		boolean isDebugNever = Boolean
				.parseBoolean(props.getCustomBundleProperty(bundleName, BUNDLE_FACTORY_CUSTOM_DEBUGNEVER, "false"));
		bundle.setDebugNever(isDebugNever);

		// Set conditional comment for IE, in case one is specified
		if (null != props.getCustomBundleProperty(bundleName, BUNDLE_FACTORY_CUSTOM_IE_CONDITIONAL_EXPRESSION))
			bundle.setIeConditionalExpression(
					props.getCustomBundleProperty(bundleName, BUNDLE_FACTORY_CUSTOM_IE_CONDITIONAL_EXPRESSION));

		// Sets the alternate URL for production mode.
		if (null != props.getCustomBundleProperty(bundleName, BUNDLE_FACTORY_CUSTOM_PRODUCTION_ALT_URL))
			bundle.setAlternateProductionURL(
					props.getCustomBundleProperty(bundleName, BUNDLE_FACTORY_CUSTOM_PRODUCTION_ALT_URL));

		boolean hasDebugURL = false;
		// Sets the debug URL for debug mode.
		if (null != props.getCustomBundleProperty(bundleName, BUNDLE_FACTORY_CUSTOM_DEBUG_URL)) {
			bundle.setDebugURL(props.getCustomBundleProperty(bundleName, BUNDLE_FACTORY_CUSTOM_DEBUG_URL));
			hasDebugURL = true;
			if (StringUtils.isEmpty(bundle.getAlternateProductionURL())) {
				throw new IllegalArgumentException("The bundle '" + bundleName
						+ "', which use a static external resource in debug mode, must use an external resource in Production mode.\n"
						+ "Please check your configuration. ");
			}
			if (isComposite) {
				throw new IllegalArgumentException("The bundle '" + bundleName
						+ "', which use a static external resource in debug mode, can't be part of a composite bundle.\n"
						+ "Please check your configuration. ");
			}
			if (StringUtils.isNotEmpty(props.getCustomBundleProperty(bundleName, BUNDLE_FACTORY_CUSTOM_MAPPINGS))) {
				throw new IllegalArgumentException("The bundle '" + bundleName
						+ "', which use a static external resource in debug mode, can't have a bundle mapping.\n"
						+ "Please check your configuration. ");
			}
		}

		if (isComposite) {
			String childBundlesProperty = props.getCustomBundleProperty(bundleName,
					BUNDLE_FACTORY_CUSTOM_COMPOSITE_NAMES);
			if (null == childBundlesProperty)
				throw new IllegalArgumentException(
						"No child bundle names were defined for the composite bundle with name:" + bundleName
								+ ". Please specify at least one in configuration. ");

			bundle.setComposite(true);

			// add children
			List<ResourceBundleDefinition> children = new ArrayList<>();
			StringTokenizer tk = new StringTokenizer(childBundlesProperty, JawrConstant.COMMA_SEPARATOR);
			while (tk.hasMoreTokens()) {
				ResourceBundleDefinition childDef = buildCustomBundleDefinition(tk.nextToken().trim(), true);
				childDef.setBundleId(props.getCustomBundleProperty(childDef.getBundleName(), BUNDLE_FACTORY_CUSTOM_ID));
				if (StringUtils.isEmpty(childDef.getDebugURL())) {
					children.add(childDef);
				} else {
					throw new IllegalArgumentException("The external bundle '" + childDef.getBundleName()
							+ "' can't be a child of the composite bundle '" + bundleName
							+ "'. Please check your configuration. ");

				}
			}
			bundle.setChildren(children);
		} else {
			String mappingsProperty = props.getCustomBundleProperty(bundleName, BUNDLE_FACTORY_CUSTOM_MAPPINGS);
			if (!hasDebugURL) {
				if (null == mappingsProperty) {
					throw new IllegalArgumentException("No mappings were defined for the bundle with name:" + bundleName
							+ ". Please specify at least one in configuration. ");
				} else {
					// Add the mappings
					List<String> mappings = new ArrayList<>();

					StringTokenizer tk = new StringTokenizer(mappingsProperty, JawrConstant.COMMA_SEPARATOR);
					while (tk.hasMoreTokens()) {
						String mapping = tk.nextToken().trim();
						mappings.add(mapping);
					}
					bundle.setMappings(mappings);
				}
			}
		}

		// dependencies
		List<String> dependencies = props.getCustomBundlePropertyAsList(bundleName, BUNDLE_FACTORY_CUSTOM_DEPENDENCIES);
		bundle.setDependencies(dependencies);

		return bundle;
	}
137073
@Route(method = HttpMethod.POST, uri = "")
    public Result installBundle(@FormParameter("bundle") final FileItem bundle,
                                @FormParameter("start") @DefaultValue("false") final boolean startIfNeeded) {
        if (bundle != null) {
            return async(new Callable<Result>() {
                @Override
                public Result call() throws Exception {
                    Bundle b;

                    try {
                        b = context.installBundle("file/temp/" + bundle.name(), bundle.stream());
                        logger().info("Bundle {} installed", b.getSymbolicName());
                    } catch (BundleException e) {
                        flash("error", "Cannot install bundle '" + bundle.name() + "' : "
                                + e.getMessage());
                        return bundle();
                    }

                    if (startIfNeeded  && ! isFragment(b)) {
                        try {
                            b.start();
                            flash("success", "Bundle '" + b.getSymbolicName() + "' installed and started");
                            return bundle();
                        } catch (BundleException e) {
                            flash("error", "Bundle '" + b.getSymbolicName() + "' installed but " +
                                    "failed to start: " + e.getMessage());
                            return bundle();
                        }
                    } else {
                        flash("success", "Bundle '" + b.getSymbolicName() + "' installed.");
                        return bundle();
                    }
                }
            });
        } else {
            logger().error("No bundle to install");
            flash("error", "Unable to install the bundle - no uploaded file");
            return bundle();
        }
    }
383225
public static BIF use(PageContext pc, String className, String bundleName, String bundleVersion) throws PageException {
	String id = toId(className, bundleName, bundleVersion);
	BIF bif = map.get(id);
	if (bif != null) return bif;

	try {
	    Class<?> clazz;
	    // OSGi bundle
	    if (!StringUtil.isEmpty(bundleName)) clazz = ClassUtil.loadClassByBundle(className, bundleName, bundleVersion, pc.getConfig().getIdentification());
	    // JAR
	    else clazz = ClassUtil.loadClass(className);

	    if (Reflector.isInstaneOf(clazz, BIF.class)) bif = (BIF) clazz.newInstance();
	    else bif = new BIFProxy(clazz);
	}
	catch (Exception e) {
	    throw Caster.toPageException(e);
	}
	map.put(id, bif);
	return bif;
    }
741737
public TaskResult add(String key, Bundle value) {
    mBundle.putBundle(key, value);
    return this;
  }
496343
private static List<String> getBundleNames(List<JoinableResourceBundle> bundles) {

		List<String> bundleNames = new ArrayList<>();
		for (Iterator<JoinableResourceBundle> iterator = bundles.iterator(); iterator.hasNext();) {
			bundleNames.add(iterator.next().getName());
		}
		return bundleNames;
	}
97885
public Extension createExtension(final Bundle bundle) {
		NullArgumentException.validateNotNull(bundle, "Bundle");
		if (bundle.getState() != Bundle.ACTIVE) {
			LOG.debug("Bundle is not in ACTIVE state, ignore it!");
			return null;
		}

		// Check compatibility
		Boolean canSeeServletClass = canSeeClass(bundle, Servlet.class);
		if (Boolean.FALSE.equals(canSeeServletClass)) {
			LOG.debug(
					"Ignore bundle {} which is not compatible with this extender",
					bundle);
			return null;
		}

		// Check that this is a web bundle
		String contextName = getHeader(bundle, "Web-ContextPath");
		if (contextName == null) {
			return null;
		}
		contextName = contextName.trim();
		if (contextName.startsWith("/")) {
			contextName = contextName.substring(1);
		}

		// Start web app creation
		final WebApp webApp = new WebApp();
		webApp.setDeploymentState(WebEvent.UNDEPLOYED);
		webApp.setBundle(bundle);
		webApp.setContextName(contextName);
		try {
			eventDispatcher.webEvent(webApp, WebEvent.DEPLOYING);
			parser.parse(bundle, webApp);

			String requireCapabilityHeader = ManifestUtil.getHeader(bundle,
					"Require-Capability");
			// If the header isn't present Pax-Web is able to take care of it.
			// otherwise needs support by Pax-CDI
			if (requireCapabilityHeader == null
					|| !requireCapabilityHeader.contains("osgi.extender=pax.cdi")) {
				webApp.setHasDependencies(true);
				dependencyManager.addWebApp(webApp);
			}

			// Register the web app
			synchronized (webApps) {
				webApps.put(bundle.getBundleId(), webApp);
			}

			return new SimpleExtension(bundle) {
				@Override
				public void doStart() {
					// Check if the web app has already been destroyed
					synchronized (webApps) {
						if (!webApps.containsKey(bundle.getBundleId())) {
							return;
						}
					}
					if (Optional.ofNullable(ManifestUtil.getHeader(bundle, "Webapp-Deploy")).orElse("true").equals("true")) {
	                    deploy(webApp);
	                } else {
	                    eventDispatcher.webEvent(new WebEvent(WebEvent.UNDEPLOYED,"/" + webApp.getContextName(), webApp.getBundle(),
	                            bundleContext.getBundle()));
	                }
				}

				@Override
				public void doDestroy() {
					// Flag this web app has destroyed by removing it
					// from the list
					synchronized (webApps) {
						webApps.remove(bundle.getBundleId());
					}
					dependencyManager.removeWebApp(webApp);
					undeploy(webApp);
					eventDispatcher.removeWebApp(webApp);
				}
			};
			//CHECKSTYLE:OFF
		} catch (Exception e) {
			LOG.error(
					"Error scanning web bundle " + bundle + ": "
							+ e.getMessage(), e);
			eventDispatcher.webEvent(webApp, WebEvent.FAILED, e);
			return null;
		}
		//CHECKSTYLE:ON
	}
539153
public Bundle getBundle(String symbolicName) {
		final String sourceMethod = "getBundle"; //$NON-NLS-1$
		boolean isTraceLogging = log.isLoggable(Level.FINER);
		if (isTraceLogging) {
			log.entering(FrameworkWiringBundleResolver.class.getName(), sourceMethod, new Object[]{symbolicName});
		}
		Bundle[] candidates = Platform.getBundles(symbolicName, null);
		if (isTraceLogging) {
			log.finer("candidate bundles = " + Arrays.toString(candidates)); //$NON-NLS-1$
		}
		if (candidates == null || candidates.length == 0) {
			if (isTraceLogging) {
				log.exiting(FrameworkWiringBundleResolver.class.getName(), sourceMethod, null);
			}
			return null;
		}
		if (candidates.length == 1) {
			// Only one choice, so return it
			if (isTraceLogging) {
				log.exiting(FrameworkWiringBundleResolver.class.getName(), sourceMethod, candidates[0]);
			}
			return candidates[0];
		}
		if (fw == null) {
			if (isTraceLogging) {
				log.finer("Framework wiring not available.  Returning bundle with latest version"); //$NON-NLS-1$
				log.exiting(FrameworkWiringBundleResolver.class.getName(), sourceMethod, candidates[0]);
			}
			return candidates[0];
		}
		Bundle result = null;
		// For each candidate bundle, check to see if it's wired, directly or indirectly, to the contributing bundle
		for (Bundle candidate : candidates) {
			Collection<Bundle> bundles = fw.getDependencyClosure(Arrays.asList(new Bundle[]{candidate}));
			for (Bundle bundle : bundles) {
				if (bundle.equals(contributingBundle)) {
					// found wired bundle.  Return the candidate.
					result = candidate;
					break;
				}
			}
			if (result != null) {
				break;
			}
		}
		if (result == null) {
			if (isTraceLogging) {
				log.finer("No wired bundle found amoung candidates.  Returning bundle with most recent version."); //$NON-NLS-1$
			}
			result =  candidates[0];
		}
		if (isTraceLogging) {
			log.exiting(FrameworkWiringBundleResolver.class.getName(), sourceMethod, result);
		}
		return result;
	}
468738
public static BundleContext getBundleContextByBundleId(long bundleId) {
        Bundle bundle = bundleContext.getBundle(bundleId);
        if (bundle != null) {
            return bundle.getBundleContext();
        } else {
            return null;
        }
    }
183748
@Override
	public BundlePathMapping build(List<String> strPathMappings) {

		BundlePathMapping bundlePathMapping = new BundlePathMapping(bundle);
		for (JoinableResourceBundle child : ((CompositeResourceBundle) bundle).getChildBundles()) {
			if (!child.getInclusionPattern().isIncludeOnlyOnDebug()) {
				bundlePathMapping.getItemPathList().addAll(child.getItemPathList());
				addFilePathMapping(bundlePathMapping, child.getItemPathList());
			}

			if (!child.getInclusionPattern().isExcludeOnDebug()) {
				bundlePathMapping.getItemDebugPathList().addAll(child.getItemDebugPathList());
				addFilePathMapping(bundlePathMapping, child.getItemDebugPathList());
			}
			bundlePathMapping.getLicensesPathList().addAll(child.getLicensesPathList());
			addFilePathMapping(bundlePathMapping, child.getLicensesPathList());
		}
		return bundlePathMapping;
	}
608073
private static ResourceBundle getResourceBundle(String enumClassName) {
    String bundleName = enumClassName.replace('$','_');
    return ResourceBundle.getBundle(bundleName);
  }
545018
@Override
	public List<String> getDirtyBundleNames() {

		List<String> bundleNames = new ArrayList<>();
		List<JoinableResourceBundle> bundlesToRebuild = getBundlesToRebuild();
		for (JoinableResourceBundle bundle : bundlesToRebuild) {
			bundleNames.add(bundle.getName());
		}
		return bundleNames;
	}
368096
public static Class loadClass(String className, String bundleName, String bundleVersion, Identification id) throws ClassException, BundleException {
	if (StringUtil.isEmpty(bundleName)) return loadClass(className);
	return loadClassByBundle(className, bundleName, bundleVersion, id);
    }
415262
public Bundler putBundle(String key, Bundle value) {
    bundle.putBundle(key, value);
    return this;
  }
1096962
public Bundler putString(String key, String value) {
    bundle.putString(key, value);
    return this;
  }
1276840
public static String getLocalizedBundleName(String bundleName, String localeKey) {

		String newName = bundleName;
		int idxSeparator = bundleName.lastIndexOf('.');
		if (StringUtils.isNotEmpty(localeKey) && idxSeparator != -1) {
			newName = bundleName.substring(0, idxSeparator);
			newName += '_' + localeKey;
			newName += bundleName.substring(idxSeparator);
		}

		return newName;
	}
874448
private void startBundle( final Bundle bundle )
        throws BundleException
    {
        // Don't start if bundle already active
        int bundleState = bundle.getState();
        if( bundleState == Bundle.ACTIVE )
        {
            return;
        }

        // Don't start if bundle is a fragment bundle
        Dictionary bundleHeaders = bundle.getHeaders();
        if( bundleHeaders.get( Constants.FRAGMENT_HOST ) != null )
        {
            return;
        }

        // Start bundle
        bundle.start();

        bundleState = bundle.getState();
        if( bundleState != Bundle.ACTIVE )
        {
            long bundleId = bundle.getBundleId();
            String bundleName = bundle.getSymbolicName();
            String bundleStateStr = bundleStateToString( bundleState );
            throw new BundleException(
                "Bundle (" + bundleId + ", " + bundleName + ") not started (still " + bundleStateStr + ")"
            );
        }
    }
1020162
public Set<String> getPropertyBundleNameSet() {

		Set<String> bundleNameSet = new HashSet<>();

		for (Object key : props.keySet()) {
			Matcher matcher = bundleNamePattern.matcher((String) key);
			if (matcher.matches()) {

				String id = matcher.group(2);
				bundleNameSet.add(id);
			}
		}
		return bundleNameSet;
	}