Search in sources :

Example 1 with MultiException

use of org.glassfish.hk2.api.MultiException in project graphhopper by graphhopper.

the class GraphHopperBundle method run.

@Override
public void run(GraphHopperBundleConfiguration configuration, Environment environment) {
    for (Object k : System.getProperties().keySet()) {
        if (k instanceof String && ((String) k).startsWith("graphhopper."))
            throw new IllegalArgumentException("You need to prefix system parameters with '-Ddw.graphhopper.' instead of '-Dgraphhopper.' see #1879 and #1897");
    }
    // When Dropwizard's Hibernate Validation misvalidates a query parameter,
    // a JerseyViolationException is thrown.
    // With this mapper, we use our custom format for that (backwards compatibility),
    // and also coerce the media type of the response to JSON, so we can return JSON error
    // messages from methods that normally have a different return type.
    // That's questionable, but on the other hand, Dropwizard itself does the same thing,
    // not here, but in a different place (the custom parameter parsers).
    // So for the moment we have to assume that both mechanisms
    // a) always return JSON error messages, and
    // b) there's no need to annotate the method with media type JSON for that.
    // 
    // However, for places that throw IllegalArgumentException or MultiException,
    // we DO need to use the media type JSON annotation, because
    // those are agnostic to the media type (could be GPX!), so the server needs to know
    // that a JSON error response is supported. (See below.)
    environment.jersey().register(new GHJerseyViolationExceptionMapper());
    // If the "?type=gpx" parameter is present, sets a corresponding media type header
    environment.jersey().register(new TypeGPXFilter());
    // Together, these two take care that MultiExceptions thrown from RouteResource
    // come out as JSON or GPX, depending on the media type
    environment.jersey().register(new MultiExceptionMapper());
    environment.jersey().register(new MultiExceptionGPXMessageBodyWriter());
    // This makes an IllegalArgumentException come out as a MultiException with
    // a single entry.
    environment.jersey().register(new IllegalArgumentExceptionMapper());
    final GraphHopperManaged graphHopperManaged = new GraphHopperManaged(configuration.getGraphHopperConfiguration());
    environment.lifecycle().manage(graphHopperManaged);
    final GraphHopper graphHopper = graphHopperManaged.getGraphHopper();
    environment.jersey().register(new AbstractBinder() {

        @Override
        protected void configure() {
            bind(configuration.getGraphHopperConfiguration()).to(GraphHopperConfig.class);
            bind(graphHopper).to(GraphHopper.class);
            bind(new JTSTriangulator(graphHopper.getRouterConfig())).to(Triangulator.class);
            bindFactory(PathDetailsBuilderFactoryFactory.class).to(PathDetailsBuilderFactory.class);
            bindFactory(ProfileResolverFactory.class).to(ProfileResolver.class);
            bindFactory(HasElevation.class).to(Boolean.class).named("hasElevation");
            bindFactory(LocationIndexFactory.class).to(LocationIndex.class);
            bindFactory(TranslationMapFactory.class).to(TranslationMap.class);
            bindFactory(EncodingManagerFactory.class).to(EncodingManager.class);
            bindFactory(GraphHopperStorageFactory.class).to(GraphHopperStorage.class);
            bindFactory(GtfsStorageFactory.class).to(GtfsStorage.class);
        }
    });
    environment.jersey().register(MVTResource.class);
    environment.jersey().register(NearestResource.class);
    environment.jersey().register(RouteResource.class);
    environment.jersey().register(IsochroneResource.class);
    environment.jersey().register(MapMatchingResource.class);
    if (configuration.getGraphHopperConfiguration().has("gtfs.file")) {
        // These are pt-specific implementations of /route and /isochrone, but the same API.
        // We serve them under different paths (/route-pt and /isochrone-pt), and forward
        // requests for ?vehicle=pt there.
        environment.jersey().register(new AbstractBinder() {

            @Override
            protected void configure() {
                if (configuration.getGraphHopperConfiguration().getBool("gtfs.free_walk", false)) {
                    bind(PtRouterFreeWalkImpl.class).to(PtRouter.class);
                } else {
                    bind(PtRouterImpl.class).to(PtRouter.class);
                }
            }
        });
        environment.jersey().register(PtRouteResource.class);
        environment.jersey().register(PtIsochroneResource.class);
        environment.jersey().register(PtMVTResource.class);
        environment.jersey().register(PtRedirectFilter.class);
    }
    environment.jersey().register(SPTResource.class);
    environment.jersey().register(I18NResource.class);
    environment.jersey().register(InfoResource.class);
    environment.healthChecks().register("graphhopper", new GraphHopperHealthCheck(graphHopper));
    environment.jersey().register(environment.healthChecks());
    environment.jersey().register(HealthCheckResource.class);
}
Also used : JTSTriangulator(com.graphhopper.isochrone.algorithm.JTSTriangulator) Triangulator(com.graphhopper.isochrone.algorithm.Triangulator) AbstractBinder(org.glassfish.hk2.utilities.binding.AbstractBinder) GraphHopper(com.graphhopper.GraphHopper) GraphHopperConfig(com.graphhopper.GraphHopperConfig) ProfileResolver(com.graphhopper.routing.ProfileResolver) EncodingManager(com.graphhopper.routing.util.EncodingManager) JTSTriangulator(com.graphhopper.isochrone.algorithm.JTSTriangulator) LocationIndex(com.graphhopper.storage.index.LocationIndex) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) PathDetailsBuilderFactory(com.graphhopper.util.details.PathDetailsBuilderFactory) GraphHopperHealthCheck(com.graphhopper.http.health.GraphHopperHealthCheck) TranslationMap(com.graphhopper.util.TranslationMap)

Example 2 with MultiException

use of org.glassfish.hk2.api.MultiException in project Payara by payara.

the class ResourceUtil method getMethodMetaData2.

public static MethodMetaData getMethodMetaData2(Dom parent, ConfigModel childModel, int parameterType) {
    MethodMetaData methodMetaData = new MethodMetaData();
    List<Class<?>> interfaces = new ArrayList<Class<?>>();
    Map<String, ParameterMetaData> params = new HashMap<String, ParameterMetaData>();
    try {
        Class<? extends ConfigBeanProxy> configBeanProxy = (Class<? extends ConfigBeanProxy>) childModel.classLoaderHolder.loadClass(childModel.targetTypeName);
        getInterfaces(configBeanProxy, interfaces);
        Set<String> attributeNames = childModel.getAttributeNames();
        for (String attributeName : attributeNames) {
            String methodName = ResourceUtil.getAttributeMethodName(attributeName);
            // camelCase the attributeName before passing out
            attributeName = Util.eleminateHypen(attributeName);
            ParameterMetaData parameterMetaData = params.get(attributeName);
            if (parameterMetaData == null) {
                parameterMetaData = new ParameterMetaData();
                params.put(attributeName, parameterMetaData);
            }
            // Check parent interfaces
            for (int i = interfaces.size() - 1; i >= 0; i--) {
                Class<?> intf = interfaces.get(i);
                try {
                    Method method = intf.getMethod(methodName);
                    Attribute attribute = method.getAnnotation(Attribute.class);
                    if (attribute != null) {
                        ParameterMetaData localParam = ResourceUtil.getParameterMetaData(attribute);
                        copyParameterMetaDataAttribute(localParam, parameterMetaData, Constants.DEFAULT_VALUE);
                        copyParameterMetaDataAttribute(localParam, parameterMetaData, Constants.KEY);
                        copyParameterMetaDataAttribute(localParam, parameterMetaData, Constants.TYPE);
                        copyParameterMetaDataAttribute(localParam, parameterMetaData, Constants.OPTIONAL);
                    }
                } catch (NoSuchMethodException e) {
                }
            }
            // Check ConfigBean
            try {
                Method method = configBeanProxy.getMethod(methodName);
                Attribute attribute = method.getAnnotation(Attribute.class);
                if (attribute != null) {
                    ParameterMetaData localParam = ResourceUtil.getParameterMetaData(attribute);
                    copyParameterMetaDataAttribute(localParam, parameterMetaData, Constants.DEFAULT_VALUE);
                    copyParameterMetaDataAttribute(localParam, parameterMetaData, Constants.KEY);
                    copyParameterMetaDataAttribute(localParam, parameterMetaData, Constants.TYPE);
                    copyParameterMetaDataAttribute(localParam, parameterMetaData, Constants.OPTIONAL);
                }
            } catch (NoSuchMethodException e) {
            }
            methodMetaData.putParameterMetaData(attributeName, parameterMetaData);
        }
    } catch (MultiException cnfe) {
        throw new RuntimeException(cnfe);
    }
    return methodMetaData;
}
Also used : HashMap(java.util.HashMap) Attribute(org.jvnet.hk2.config.Attribute) ArrayList(java.util.ArrayList) MethodMetaData(org.glassfish.admin.rest.provider.MethodMetaData) Method(java.lang.reflect.Method) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) MultiException(org.glassfish.hk2.api.MultiException) ParameterMetaData(org.glassfish.admin.rest.provider.ParameterMetaData)

Example 3 with MultiException

use of org.glassfish.hk2.api.MultiException in project Payara by payara.

the class ResourceUtil method getMethodMetaData.

/**
 * Constructs and returns the resource method meta-data. This method is
 * called to get meta-data in case of update method (POST).
 *
 * @param configBeanModel the config bean associated with the resource.
 * @return MethodMetaData the meta-data store for the resource method.
 */
public static MethodMetaData getMethodMetaData(ConfigModel configBeanModel) {
    MethodMetaData methodMetaData = new MethodMetaData();
    Class<? extends ConfigBeanProxy> configBeanProxy = null;
    try {
        configBeanProxy = (Class<? extends ConfigBeanProxy>) configBeanModel.classLoaderHolder.loadClass(configBeanModel.targetTypeName);
        Set<String> attributeNames = configBeanModel.getAttributeNames();
        for (String attributeName : attributeNames) {
            String methodName = getAttributeMethodName(attributeName);
            Method method = null;
            try {
                method = configBeanProxy.getMethod(methodName);
            } catch (NoSuchMethodException e) {
                // Method not found, so let's try a brute force method if the method
                // can't be found via the method above.  For example: for
                // Ssl.getSSLInactivityTimeout(), we calculate getSslInactivityTimeout,
                // which doesn't match due to case.
                String booleanMethodName = getAttributeBooleanMethodName(attributeName);
                for (Method m : configBeanProxy.getMethods()) {
                    if (m.getName().equalsIgnoreCase(methodName) || m.getName().equalsIgnoreCase(booleanMethodName)) {
                        method = m;
                    }
                }
            }
            Attribute attribute = method.getAnnotation(Attribute.class);
            if (attribute != null) {
                ParameterMetaData parameterMetaData = getParameterMetaData(attribute);
                if (method.getAnnotation(Deprecated.class) != null) {
                    parameterMetaData.putAttribute(Constants.DEPRECATED, "true");
                }
                // camelCase the attributeName before passing out
                attributeName = eleminateHypen(attributeName);
                methodMetaData.putParameterMetaData(attributeName, parameterMetaData);
            }
        }
    } catch (MultiException e) {
        LOGGER.log(Level.SEVERE, null, e);
    }
    return methodMetaData;
}
Also used : Attribute(org.jvnet.hk2.config.Attribute) MethodMetaData(org.glassfish.admin.rest.provider.MethodMetaData) Method(java.lang.reflect.Method) MultiException(org.glassfish.hk2.api.MultiException) ParameterMetaData(org.glassfish.admin.rest.provider.ParameterMetaData)

Example 4 with MultiException

use of org.glassfish.hk2.api.MultiException in project Payara by payara.

the class CommandRunnerImpl method injectParameters.

public static boolean injectParameters(final CommandModel model, final Object injectionTarget, final InjectionResolver<Param> injector, final ActionReport report) {
    if (injectionTarget instanceof GenericCrudCommand) {
        GenericCrudCommand c = GenericCrudCommand.class.cast(injectionTarget);
        c.setInjectionResolver(injector);
    }
    // inject
    try {
        injectionMgr.inject(injectionTarget, injector);
    } catch (UnsatisfiedDependencyException e) {
        Param param = e.getAnnotation(Param.class);
        CommandModel.ParamModel paramModel = null;
        for (CommandModel.ParamModel pModel : model.getParameters()) {
            if (pModel.getParam().equals(param)) {
                paramModel = pModel;
                break;
            }
        }
        String errorMsg;
        final String usage = getUsageText(model);
        if (paramModel != null) {
            String paramName = paramModel.getName();
            String paramDesc = paramModel.getLocalizedDescription();
            if (param.primary()) {
                errorMsg = adminStrings.getLocalString("commandrunner.operand.required", "Operand required.");
            } else if (param.password()) {
                errorMsg = adminStrings.getLocalString("adapter.param.missing.passwordfile", "{0} command requires the passwordfile " + "parameter containing {1} entry.", model.getCommandName(), paramName);
            } else if (paramDesc != null) {
                errorMsg = adminStrings.getLocalString("admin.param.missing", "{0} command requires the {1} parameter ({2})", model.getCommandName(), paramName, paramDesc);
            } else {
                errorMsg = adminStrings.getLocalString("admin.param.missing.nodesc", "{0} command requires the {1} parameter", model.getCommandName(), paramName);
            }
        } else {
            errorMsg = adminStrings.getLocalString("admin.param.missing.nofound", "Cannot find {1} in {0} command model, file a bug", model.getCommandName(), e.getUnsatisfiedName());
        }
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage(errorMsg);
        report.setFailureCause(e);
        ActionReport.MessagePart childPart = report.getTopMessagePart().addChild();
        childPart.setMessage(usage);
        return false;
    } catch (MultiException e) {
        // If the cause is UnacceptableValueException -- we want the message
        // from it.  It is wrapped with a less useful Exception.
        Exception exception = null;
        for (Throwable th : e.getErrors()) {
            Throwable cause = th;
            while (cause != null) {
                if ((cause instanceof UnacceptableValueException) || (cause instanceof IllegalArgumentException)) {
                    exception = (Exception) th;
                    break;
                }
                cause = cause.getCause();
            }
        }
        if (exception == null) {
            // Not an UnacceptableValueException or IllegalArgumentException
            exception = e;
        }
        logger.log(Level.SEVERE, KernelLoggerInfo.invocationException, exception);
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage(exception.getMessage());
        report.setFailureCause(exception);
        ActionReport.MessagePart childPart = report.getTopMessagePart().addChild();
        childPart.setMessage(getUsageText(model));
        return false;
    }
    checkAgainstBeanConstraints(injectionTarget, model.getCommandName());
    return true;
}
Also used : GenericCrudCommand(org.glassfish.config.support.GenericCrudCommand) UnsatisfiedDependencyException(org.jvnet.hk2.config.UnsatisfiedDependencyException) Param(org.glassfish.api.Param) UnacceptableValueException(org.glassfish.common.util.admin.UnacceptableValueException) MultiException(org.glassfish.hk2.api.MultiException) MultiException(org.glassfish.hk2.api.MultiException) UnacceptableValueException(org.glassfish.common.util.admin.UnacceptableValueException) IOException(java.io.IOException) UnsatisfiedDependencyException(org.jvnet.hk2.config.UnsatisfiedDependencyException)

Example 5 with MultiException

use of org.glassfish.hk2.api.MultiException in project Payara by payara.

the class ArchiveFactory method openArchive.

/**
 * Opens an existing archivist using the URL as the path.
 * The URL protocol will defines the type of desired archive
 * (jar, file, memory, etc...)
 * @param path url to the existing archive
 * @return the appropriate archive
 */
public ReadableArchive openArchive(URI path) throws IOException {
    String provider = path.getScheme();
    if (provider.equals("file")) {
        // this could be a jar file or a directory
        File f = new File(path);
        if (!f.exists())
            throw new FileNotFoundException(f.getPath());
        if (f.isFile()) {
            provider = "jar";
        }
    }
    try {
        ReadableArchive archive = habitat.getService(ReadableArchive.class, provider);
        if (archive == null) {
            deplLogger.log(Level.SEVERE, IMPLEMENTATION_NOT_FOUND, provider);
            throw new MalformedURLException("Protocol not supported : " + provider);
        }
        archive.open(path);
        return archive;
    } catch (MultiException e) {
        LogRecord lr = new LogRecord(Level.SEVERE, IMPLEMENTATION_NOT_FOUND);
        lr.setParameters(new Object[] { provider });
        lr.setThrown(e);
        deplLogger.log(lr);
        throw new MalformedURLException("Protocol not supported : " + provider);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) LogRecord(java.util.logging.LogRecord) FileNotFoundException(java.io.FileNotFoundException) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) File(java.io.File) MultiException(org.glassfish.hk2.api.MultiException)

Aggregations

MultiException (org.glassfish.hk2.api.MultiException)20 IOException (java.io.IOException)5 Method (java.lang.reflect.Method)5 Param (org.glassfish.api.Param)3 RemoteCLICommand (com.sun.enterprise.admin.cli.remote.RemoteCLICommand)2 GrizzlyService (com.sun.enterprise.v3.services.impl.GrizzlyService)2 MalformedURLException (java.net.MalformedURLException)2 List (java.util.List)2 LogRecord (java.util.logging.LogRecord)2 MethodMetaData (org.glassfish.admin.rest.provider.MethodMetaData)2 ParameterMetaData (org.glassfish.admin.rest.provider.ParameterMetaData)2 ActionReport (org.glassfish.api.ActionReport)2 CommandModelImpl (org.glassfish.common.util.admin.CommandModelImpl)2 Attribute (org.jvnet.hk2.config.Attribute)2 ConfigModel (org.jvnet.hk2.config.ConfigModel)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 GraphHopper (com.graphhopper.GraphHopper)1 GraphHopperConfig (com.graphhopper.GraphHopperConfig)1 GraphHopperHealthCheck (com.graphhopper.http.health.GraphHopperHealthCheck)1 JTSTriangulator (com.graphhopper.isochrone.algorithm.JTSTriangulator)1