View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.examples.rest;
18  
19  import java.net.*;
20  import java.util.*;
21  
22  import org.apache.juneau.microservice.jetty.*;
23  import org.apache.juneau.parser.*;
24  import org.apache.juneau.rest.client.*;
25  import org.apache.juneau.serializer.*;
26  
27  /**
28   * Utility class for starting up the examples microservice.
29   */
30  public class SamplesMicroservice {
31  	static volatile JettyMicroservice microservice;
32  	static URI microserviceURI;
33  
34  	// Reusable HTTP clients that get created and shut down with the microservice.
35  	public static RestClient DEFAULT_CLIENT;
36  	public static RestClient DEFAULT_CLIENT_PLAINTEXT;
37  
38  	/**
39  	 * Starts the microservice.
40  	 * @return <jk>true</jk> if the service started, <jk>false</jk> if it's already started.
41  	 * If this returns <jk>false</jk> then don't call stopMicroservice()!.
42  	 */
43  	public synchronized static boolean startMicroservice() {
44  		if (microservice != null)
45  			return false;
46  		try {
47  			Locale.setDefault(Locale.US);
48  			microservice = JettyMicroservice.create().workingDir("../juneau-examples-rest-jetty").configName("juneau-examples-rest-jetty.cfg").servlet(RootResources.class).build();
49  			microserviceURI = microservice.start().getURI();
50  			DEFAULT_CLIENT = client().json().build();
51  			DEFAULT_CLIENT_PLAINTEXT = client().plainText().build();
52  			return true;
53  		} catch (Throwable e) {
54  			// Probably already started.
55  			e.printStackTrace();
56  			System.err.println(e); // NOT DEBUG
57  			return false;
58  		}
59  	}
60  
61  	/**
62  	 * Returns the URI of the microservice.
63  	 * @return The URI of the microservice.
64  	 */
65  	public static URI getURI() {
66  		if (microservice == null)
67  			startMicroservice();
68  		return microserviceURI;
69  	}
70  
71  	/**
72  	 * Stops the microservice.
73  	 */
74  	public synchronized static void stopMicroservice() {
75  		try {
76  			microservice.stop();
77  			microservice = null;
78  			DEFAULT_CLIENT.closeQuietly();
79  			DEFAULT_CLIENT_PLAINTEXT.closeQuietly();
80  		} catch (Exception e) {
81  			System.err.println(e); // NOT DEBUG
82  		}
83  	}
84  
85  	/**
86  	 * Create a new HTTP client.
87  	 */
88  	public static RestClient.Builder client() {
89  		try {
90  			return RestClient.create().rootUrl(microserviceURI);
91  		} catch (Exception e) {
92  			throw new IllegalStateException(e);
93  		}
94  	}
95  
96  	/**
97  	 * Create a new HTTP client using the specified serializer and parser.
98  	 */
99  	public static RestClient.Builder client(Serializer s, Parser p) {
100 		return client().serializer(s).parser(p);
101 	}
102 
103 	/**
104 	 * Create a new HTTP client using the specified serializer and parser.
105 	 */
106 	public static RestClient.Builder client(Class<? extends Serializer> s, Class<? extends Parser> p) {
107 		return client().serializer(s).parser(p);
108 	}
109 }