|
Java example source code file (UiServer.java)
The UiServer.java Java example source code/* * * * Copyright 2015 Skymind,Inc. * * * * Licensed 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.deeplearning4j.ui; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.module.SimpleModule; import com.google.common.collect.ImmutableMap; import io.dropwizard.Application; import io.dropwizard.assets.AssetsBundle; import io.dropwizard.jersey.jackson.JsonProcessingExceptionMapper; import io.dropwizard.jetty.HttpConnectorFactory; import io.dropwizard.lifecycle.ServerLifecycleListener; import io.dropwizard.server.DefaultServerFactory; import io.dropwizard.setup.Bootstrap; import io.dropwizard.setup.Environment; import io.dropwizard.views.ViewBundle; import org.apache.commons.io.IOUtils; import org.canova.api.util.ClassPathResource; import org.deeplearning4j.ui.activation.ActivationsDropwiz; import org.deeplearning4j.ui.activation.ActivationsResource; import org.deeplearning4j.ui.api.ApiResource; import org.deeplearning4j.ui.defaults.DefaultDropwiz; import org.deeplearning4j.ui.defaults.DefaultResource; import org.deeplearning4j.ui.exception.GenericExceptionMapper; import org.deeplearning4j.ui.flow.FlowDropwiz; import org.deeplearning4j.ui.flow.FlowResource; import org.deeplearning4j.ui.nearestneighbors.NearestNeighborsResource; import org.deeplearning4j.ui.nearestneighbors.word2vec.NearestNeighborsDropwiz; import org.deeplearning4j.ui.renders.RendersDropwiz; import org.deeplearning4j.ui.renders.RendersResource; import org.deeplearning4j.ui.tsne.TsneDropwiz; import org.deeplearning4j.ui.tsne.TsneResource; import org.deeplearning4j.ui.weights.WeightDropwiz; import org.deeplearning4j.ui.weights.WeightResource; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.servlets.CrossOriginFilter; import org.glassfish.jersey.media.multipart.MultiPartFeature; import org.nd4j.linalg.api.ndarray.INDArray; import org.nd4j.serde.jackson.VectorDeSerializer; import org.nd4j.serde.jackson.VectorSerializer; import javax.servlet.DispatcherType; import javax.servlet.FilterRegistration; import java.io.*; import java.util.EnumSet; /** * @author Adam Gibson */ public class UiServer extends Application<UIConfiguration> { private static UiServer INSTANCE; private UIConfiguration conf; private Environment env; public UiServer() { INSTANCE = this; } private int port; public int getPort(){ return port; } public static synchronized UiServer getInstance() throws Exception { if(INSTANCE == null) createServer(); return INSTANCE; } public Environment getEnv() { return env; } public UiConnectionInfo getConnectionInfo() { UiConnectionInfo info = new UiConnectionInfo.Builder() .setPort(getPort()) .setAddress("localhost") .enableHttps(false) .build(); return info; } @Override public void run(UIConfiguration uiConfiguration, Environment environment) throws Exception { this.conf = uiConfiguration; this.env = environment; //Workaround to dropwizard sometimes ignoring ports specified in dropwizard.yml int[] portsFromYml = getApplicationPortFromYml(); if(portsFromYml[0] != -1 ) { ((HttpConnectorFactory) ((DefaultServerFactory) uiConfiguration.getServerFactory()) .getApplicationConnectors().get(0)).setPort(portsFromYml[0]); } if(portsFromYml[1] != -1 ) { ((HttpConnectorFactory) ((DefaultServerFactory) uiConfiguration.getServerFactory()) .getAdminConnectors().get(0)).setPort(portsFromYml[1]); } //Read the port that actually got assigned (needed for random ports i.e. port: 0 setting in yml) environment.lifecycle().addServerLifecycleListener(new ServerLifecycleListener() { @Override public void serverStarted(Server server) { for (Connector connector : server.getConnectors()) { if (connector instanceof ServerConnector) { ServerConnector serverConnector = (ServerConnector) connector; if(!serverConnector.getName().toLowerCase().contains("application")) continue; int port = serverConnector.getLocalPort(); try{ UiServer.getInstance().port = port; }catch( Exception e ){ e.printStackTrace(); } } } } }); environment.jersey().register(MultiPartFeature.class); environment.jersey().register(new GenericExceptionMapper()); environment.jersey().register(new JsonProcessingExceptionMapper()); environment.jersey().register(new TsneDropwiz(conf.getUploadPath())); // environment.jersey().register(new NearestNeighborsDropwiz()); environment.jersey().register(new DefaultDropwiz()); environment.jersey().register(new WeightDropwiz()); environment.jersey().register(new ActivationsDropwiz()); environment.jersey().register(new RendersDropwiz()); environment.jersey().register(new ApiResource()); environment.jersey().register(new GenericExceptionMapper()); environment.jersey().register(new FlowDropwiz()); environment.jersey().register(new org.deeplearning4j.ui.nearestneighbors.word2vec.NearestNeighborsDropwiz(conf.getUploadPath())); environment.getObjectMapper().configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); environment.getObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); environment.getObjectMapper().registerModule(module()); configureCors(environment); } @Override public void initialize(Bootstrap<UIConfiguration> bootstrap) { //custom serializers for the json serde bootstrap.getObjectMapper().registerModule(module()); bootstrap.addBundle(new ViewBundle<UIConfiguration>() { @Override public ImmutableMap<String, ImmutableMap Other Java examples (source code examples)Here is a short list of links related to this Java UiServer.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.