Example #1
0
  public static void main(String[] args) throws Exception {
    int port = 7070;
    if (args.length >= 1) {
      port = Integer.parseInt(args[0]);
    }

    // test_case_data/sandbox/ contains HDP 2.2 site xmls which is dev sandbox
    ClasspathUtil.addClasspath(new File("../examples/test_case_data/sandbox").getAbsolutePath());
    System.setProperty(KylinConfig.KYLIN_CONF, "../examples/test_case_data/sandbox");
    System.setProperty("hdp.version", "2.2.0.0-2041"); // mapred-site.xml ref this

    // workaround for job submission from win to linux --
    // https://issues.apache.org/jira/browse/MAPREDUCE-4052
    if (Shell.WINDOWS) {
      {
        Field field = Shell.class.getDeclaredField("WINDOWS");
        field.setAccessible(true);
        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
        field.set(null, false);
      }
      {
        Field field = java.io.File.class.getDeclaredField("pathSeparator");
        field.setAccessible(true);
        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
        field.set(null, ":");
      }
    }

    System.setProperty("spring.profiles.active", "testing");
    String webBase = new File("../webapp/app").getAbsolutePath();
    if (new File(webBase, "WEB-INF").exists() == false) {
      throw new RuntimeException(
          "In order to launch Kylin web app from IDE, please make a symblink from webapp/app/WEB-INF to server/src/main/webapp/WEB-INF");
    }

    Tomcat tomcat = new Tomcat();
    tomcat.setPort(port);
    tomcat.setBaseDir(".");

    // Add AprLifecycleListener
    StandardServer server = (StandardServer) tomcat.getServer();
    AprLifecycleListener listener = new AprLifecycleListener();
    server.addLifecycleListener(listener);

    Context webContext = tomcat.addWebapp("/kylin", webBase);
    ErrorPage notFound = new ErrorPage();
    notFound.setErrorCode(404);
    notFound.setLocation("/index.html");
    webContext.addErrorPage(notFound);
    webContext.addWelcomeFile("index.html");

    // tomcat start
    tomcat.start();
    tomcat.getServer().await();
  }
  @Before
  @Override
  public void setUp() throws Exception {
    super.setUp();

    // Trigger loading of catalina.properties
    CatalinaProperties.getProperty("foo");

    File appBase = new File(getTemporaryDirectory(), "webapps");
    if (!appBase.exists() && !appBase.mkdir()) {
      fail("Unable to create appBase for test");
    }

    tomcat = new TomcatWithFastSessionIDs();

    String protocol = getProtocol();
    Connector connector = new Connector(protocol);
    // Listen only on localhost
    connector.setAttribute("address", InetAddress.getByName("localhost").getHostAddress());
    // Use random free port
    connector.setPort(0);
    // Mainly set to reduce timeouts during async tests
    connector.setAttribute("connectionTimeout", "3000");
    tomcat.getService().addConnector(connector);
    tomcat.setConnector(connector);

    // Add AprLifecycleListener if we are using the Apr connector
    if (protocol.contains("Apr")) {
      StandardServer server = (StandardServer) tomcat.getServer();
      AprLifecycleListener listener = new AprLifecycleListener();
      listener.setSSLRandomSeed("/dev/urandom");
      server.addLifecycleListener(listener);
      connector.setAttribute("pollerThreadCount", Integer.valueOf(1));
    }

    File catalinaBase = getTemporaryDirectory();
    tomcat.setBaseDir(catalinaBase.getAbsolutePath());
    tomcat.getHost().setAppBase(appBase.getAbsolutePath());

    accessLogEnabled = Boolean.parseBoolean(System.getProperty("tomcat.test.accesslog", "false"));
    if (accessLogEnabled) {
      AccessLogValve alv = new AccessLogValve();
      alv.setDirectory(getBuildDirectory() + "/logs");
      alv.setPattern("%h %l %u %t \"%r\" %s %b %I %D");
      tomcat.getHost().getPipeline().addValve(alv);
    }

    // Cannot delete the whole tempDir, because logs are there,
    // but delete known subdirectories of it.
    addDeleteOnTearDown(new File(catalinaBase, "webapps"));
    addDeleteOnTearDown(new File(catalinaBase, "work"));
  }
 private void bindResource(ResourceBase res) {
   try {
     Context globalNamingContext = standardServer.getGlobalNamingContext();
     Object value = globalNamingContext.lookup(res.getName());
     String type = res.getType();
     bindResource(res.getName(), value, type);
   } catch (NamingException e) {
     logger.error("Unable to lookup Global Tomcat resource " + res.getName(), e);
   }
 }
 /** {@inheritDoc} */
 @Override
 public synchronized void stop(StopContext context) {
   try {
     server.stop();
   } catch (Exception e) {
   }
   //        engine = null;
   //        service = null;
   server = null;
   sipEngine = null;
   sipService = null;
 }
  /**
   * Acknowledge the occurrence of the specified event.
   *
   * @param event LifecycleEvent that has occurred
   */
  @Override
  public void lifecycleEvent(LifecycleEvent event) {

    container = event.getLifecycle();

    if (container instanceof Context) {
      namingResources = ((Context) container).getNamingResources();
      logger = log;
      token = ((Context) container).getNamingToken();
    } else if (container instanceof Server) {
      namingResources = ((Server) container).getGlobalNamingResources();
      token = ((Server) container).getNamingToken();
    } else {
      return;
    }

    if (Lifecycle.CONFIGURE_START_EVENT.equals(event.getType())) {

      if (initialized) return;

      try {
        Hashtable<String, Object> contextEnv = new Hashtable<>();
        namingContext = new NamingContext(contextEnv, getName());
        ContextAccessController.setSecurityToken(getName(), token);
        ContextAccessController.setSecurityToken(container, token);
        ContextBindings.bindContext(container, namingContext, token);
        if (log.isDebugEnabled()) {
          log.debug("Bound " + container);
        }

        // Configure write when read-only behaviour
        namingContext.setExceptionOnFailedWrite(getExceptionOnFailedWrite());

        // Setting the context in read/write mode
        ContextAccessController.setWritable(getName(), token);

        try {
          createNamingContext();
        } catch (NamingException e) {
          logger.error(sm.getString("naming.namingContextCreationFailed", e));
        }

        namingResources.addPropertyChangeListener(this);

        // Binding the naming context to the class loader
        if (container instanceof Context) {
          // Setting the context in read only mode
          ContextAccessController.setReadOnly(getName());
          try {
            ContextBindings.bindClassLoader(
                container, token, ((Context) container).getLoader().getClassLoader());
          } catch (NamingException e) {
            logger.error(sm.getString("naming.bindFailed", e));
          }
        }

        if (container instanceof Server) {
          org.apache.naming.factory.ResourceLinkFactory.setGlobalContext(namingContext);
          try {
            ContextBindings.bindClassLoader(container, token, this.getClass().getClassLoader());
          } catch (NamingException e) {
            logger.error(sm.getString("naming.bindFailed", e));
          }
          if (container instanceof StandardServer) {
            ((StandardServer) container).setGlobalNamingContext(namingContext);
          }
        }

      } finally {
        // Regardless of success, so that we can do cleanup on configure_stop
        initialized = true;
      }

    } else if (Lifecycle.CONFIGURE_STOP_EVENT.equals(event.getType())) {

      if (!initialized) return;

      try {
        // Setting the context in read/write mode
        ContextAccessController.setWritable(getName(), token);
        ContextBindings.unbindContext(container, token);

        if (container instanceof Context) {
          ContextBindings.unbindClassLoader(
              container, token, ((Context) container).getLoader().getClassLoader());
        }

        if (container instanceof Server) {
          namingResources.removePropertyChangeListener(this);
          ContextBindings.unbindClassLoader(container, token, this.getClass().getClassLoader());
        }

        ContextAccessController.unsetSecurityToken(getName(), token);
        ContextAccessController.unsetSecurityToken(container, token);

        // unregister mbeans.
        if (!objectNames.isEmpty()) {
          Collection<ObjectName> names = objectNames.values();
          Registry registry = Registry.getRegistry(null, null);
          for (ObjectName objectName : names) {
            registry.unregisterComponent(objectName);
          }
        }
      } finally {
        objectNames.clear();

        namingContext = null;
        envCtx = null;
        compCtx = null;
        initialized = false;
      }
    }
  }
 public OpenEJBNamingContextListener(StandardServer standardServer) {
   this.standardServer = standardServer;
   namingResources = standardServer.getGlobalNamingResources();
 }
  /** {@inheritDoc} */
  public synchronized void start(StartContext context) throws StartException {
    if (org.apache.tomcat.util.Constants.ENABLE_MODELER) {
      // Set the MBeanServer
      final MBeanServer mbeanServer = this.mbeanServer.getOptionalValue();
      if (mbeanServer != null) {
        Registry.getRegistry(null, null).setMBeanServer(mbeanServer);
      }
    }

    System.setProperty(
        "catalina.home", pathManagerInjector.getValue().getPathEntry(TEMP_DIR).resolvePath());
    server = new StandardServer();

    //        final StandardService service = new StandardService();
    //        service.setName(JBOSS_SIP);
    //        service.setServer(server);
    //        server.addService(service);

    //        final Engine engine = new StandardEngine();
    //        engine.setName(JBOSS_SIP);
    //        engine.setService(service);
    //        engine.setDefaultHost(defaultHost);
    //        if (instanceId != null) {
    //            engine.setJvmRoute(instanceId);
    //        }
    //
    //        service.setContainer(engine);

    //        if (useNative) {
    //            final AprLifecycleListener apr = new AprLifecycleListener();
    //            apr.setSSLEngine("on");
    //            server.addLifecycleListener(apr);
    //        }
    //        server.addLifecycleListener(new JasperListener());

    sipService = new SipStandardService();
    // https://code.google.com/p/sipservlets/issues/detail?id=277
    // Add the Service and sip app dispatched right away so apps can get the needed objects
    // when they deploy fast
    server.addService(sipService);

    if (sipAppDispatcherClass != null) {
      sipService.setSipApplicationDispatcherClassName(sipAppDispatcherClass);
    } else {
      sipService.setSipApplicationDispatcherClassName(SipApplicationDispatcherImpl.class.getName());
    }
    //
    final String baseDir = System.getProperty("jboss.server.base.dir");
    if (sipAppRouterFile != null) {
      if (!sipAppRouterFile.startsWith(FILE_PREFIX_PATH)) {
        sipAppRouterFile = FILE_PREFIX_PATH.concat(baseDir).concat("/").concat(sipAppRouterFile);
      }
      System.setProperty("javax.servlet.sip.dar", sipAppRouterFile);
    }
    //
    sipService.setSipPathName(sipPathName);
    //
    if (sipStackPropertiesFile != null) {
      if (!sipStackPropertiesFile.startsWith(FILE_PREFIX_PATH)) {
        sipStackPropertiesFile =
            FILE_PREFIX_PATH.concat(baseDir).concat("/").concat(sipStackPropertiesFile);
      }
    }
    sipService.setSipStackPropertiesFile(sipStackPropertiesFile);
    //
    if (sipConcurrencyControlMode != null) {
      sipService.setConcurrencyControlMode(sipConcurrencyControlMode);
    } else {
      sipService.setConcurrencyControlMode("None");
    }
    //
    sipService.setCongestionControlCheckingInterval(sipCongestionControlInterval);
    //
    sipService.setUsePrettyEncoding(usePrettyEncoding);
    //
    sipService.setBaseTimerInterval(baseTimerInterval);
    sipService.setT2Interval(t2Interval);
    sipService.setT4Interval(t4Interval);
    sipService.setTimerDInterval(timerDInterval);
    if (additionalParameterableHeaders != null) {
      sipService.setAdditionalParameterableHeaders(additionalParameterableHeaders);
    }
    sipService.setDialogPendingRequestChecking(dialogPendingRequestChecking);
    sipService.setDnsServerLocatorClass(dnsServerLocatorClass);
    sipService.setDnsTimeout(dnsTimeout);
    sipService.setDnsResolverClass(dnsResolverClass);
    sipService.setCanceledTimerTasksPurgePeriod(canceledTimerTasksPurgePeriod);
    sipService.setMemoryThreshold(memoryThreshold);
    sipService.setBackToNormalMemoryThreshold(backToNormalMemoryThreshold);
    sipService.setCongestionControlPolicy(congestionControlPolicy);
    sipService.setOutboundProxy(outboundProxy);
    sipService.setName(JBOSS_SIP);
    sipService.setServer(server);

    sipEngine = new SipStandardEngine();
    sipEngine.setName(JBOSS_SIP);
    sipEngine.setService(sipService);
    //        sipEngine.setDefaultHost(defaultHost);
    if (instanceId != null) {
      sipEngine.setJvmRoute(instanceId);
    }
    sipService.setContainer(sipEngine);

    try {
      server.init();
      server.start();
    } catch (Exception e) {
      throw new StartException(MESSAGES.errorStartingSip(), e);
    }
  }
  /**
   * Acknowledge the occurrence of the specified event.
   *
   * @param event LifecycleEvent that has occurred
   */
  public void lifecycleEvent(LifecycleEvent event) {

    container = event.getLifecycle();

    if (container instanceof Context) {
      namingResources = ((Context) container).getNamingResources();
    } else if (container instanceof Server) {
      namingResources = ((Server) container).getGlobalNamingResources();
    } else {
      return;
    }

    if (event.getType() == Lifecycle.START_EVENT) {

      if (initialized) return;

      Hashtable contextEnv = new Hashtable();
      try {
        namingContext = new NamingContext(contextEnv, getName());
      } catch (NamingException e) {
        // Never happens
      }
      ContextAccessController.setSecurityToken(getName(), container);
      ContextBindings.bindContext(container, namingContext, container);

      // Setting the context in read/write mode
      ContextAccessController.setWritable(getName(), container);

      try {
        createNamingContext();
      } catch (NamingException e) {
        log(sm.getString("naming.namingContextCreationFailed", e));
      }

      // Binding the naming context to the class loader
      if (container instanceof Context) {
        // Setting the context in read only mode
        ContextAccessController.setReadOnly(getName());
        try {
          ContextBindings.bindClassLoader(
              container, container, ((Container) container).getLoader().getClassLoader());
        } catch (NamingException e) {
          log(sm.getString("naming.bindFailed", e));
        }
      }

      if (container instanceof Server) {
        namingResources.addPropertyChangeListener(this);
        org.apache.naming.factory.ResourceLinkFactory.setGlobalContext(namingContext);
        try {
          ContextBindings.bindClassLoader(container, container, this.getClass().getClassLoader());
        } catch (NamingException e) {
          log(sm.getString("naming.bindFailed", e));
        }
        if (container instanceof StandardServer) {
          ((StandardServer) container).setGlobalNamingContext(namingContext);
        }
      }

      initialized = true;

    } else if (event.getType() == Lifecycle.STOP_EVENT) {

      if (!initialized) return;

      // Setting the context in read/write mode
      ContextAccessController.setWritable(getName(), container);

      if (container instanceof Context) {
        ContextBindings.unbindClassLoader(
            container, container, ((Container) container).getLoader().getClassLoader());
      }

      if (container instanceof Server) {
        namingResources.removePropertyChangeListener(this);
        ContextBindings.unbindClassLoader(container, container, this.getClass().getClassLoader());
      }

      ContextAccessController.unsetSecurityToken(getName(), container);

      namingContext = null;
      envCtx = null;
      compCtx = null;
      initialized = false;
    }
  }