org.apache.tamaya.vertx.ConfiguredVerticleFactory.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.tamaya.vertx.ConfiguredVerticleFactory.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.tamaya.vertx;

import io.vertx.core.Verticle;
import io.vertx.core.impl.verticle.CompilingClassLoader;
import io.vertx.core.spi.VerticleFactory;
import org.apache.tamaya.inject.ConfigurationInjector;

/**
 * Verticle factory that produces Java verticles that are configured with the Tamaya injection API.
 */
public class ConfiguredVerticleFactory implements VerticleFactory {

    @Override
    public String prefix() {
        return "java";
    }

    @Override
    public int order() {
        return 1;
    }

    @Override
    public Verticle createVerticle(String verticleName, ClassLoader classLoader) throws Exception {
        verticleName = VerticleFactory.removePrefix(verticleName);
        Class clazz;
        if (verticleName.endsWith(".java")) {
            CompilingClassLoader compilingLoader = new CompilingClassLoader(classLoader, verticleName);
            String className = compilingLoader.resolveMainClassName();
            clazz = compilingLoader.loadClass(className);
        } else {
            clazz = classLoader.loadClass(verticleName);
        }

        Verticle instance = (Verticle) clazz.getConstructor().newInstance();
        ConfigurationInjector.getInstance(classLoader).configure(instance);
        return instance;
    }
}