blob: 1f5fbd48475e054dd0a9546fe7bc0aaeac2f9409 [file] [log] [blame]
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.ui;
17
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -070018import org.slf4j.Logger;
19import org.slf4j.LoggerFactory;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080020
21import java.io.InputStream;
Simon Hunte05cae42015-07-23 17:35:24 -070022import java.util.ArrayList;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080023import java.util.List;
24
Simon Hunte05cae42015-07-23 17:35:24 -070025import static com.google.common.base.Preconditions.checkArgument;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080026import static com.google.common.base.Preconditions.checkNotNull;
27
28/**
29 * User interface extension.
30 */
Simon Hunte05cae42015-07-23 17:35:24 -070031public final class UiExtension {
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080032
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -070033 private final Logger log = LoggerFactory.getLogger(getClass());
34
Thomas Vachuska529db0a2015-02-23 16:42:14 -080035 private static final String VIEW_PREFIX = "app/view/";
Simon Hunte05cae42015-07-23 17:35:24 -070036 private static final String EMPTY = "";
37 private static final String SLASH = "/";
38 private static final String CSS_HTML = "css.html";
39 private static final String JS_HTML = "js.html";
Thomas Vachuska529db0a2015-02-23 16:42:14 -080040
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080041 private final ClassLoader classLoader;
Simon Hunte05cae42015-07-23 17:35:24 -070042 private final String resourcePath;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080043 private final List<UiView> views;
Thomas Vachuska3553b302015-03-07 14:49:43 -080044 private final UiMessageHandlerFactory messageHandlerFactory;
Simon Hunte05cae42015-07-23 17:35:24 -070045 private final UiTopoOverlayFactory topoOverlayFactory;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080046
Simon Hunte05cae42015-07-23 17:35:24 -070047
48 // private constructor - only the builder calls this
49 private UiExtension(ClassLoader cl, String path, List<UiView> views,
50 UiMessageHandlerFactory mhFactory,
51 UiTopoOverlayFactory toFactory) {
52 this.classLoader = cl;
53 this.resourcePath = path;
54 this.views = views;
55 this.messageHandlerFactory = mhFactory;
56 this.topoOverlayFactory = toFactory;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080057 }
58
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080059
60 /**
61 * Returns input stream containing CSS inclusion statements.
62 *
63 * @return CSS inclusion statements
64 */
65 public InputStream css() {
Simon Hunte05cae42015-07-23 17:35:24 -070066 return getStream(resourcePath + CSS_HTML);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080067 }
68
69 /**
70 * Returns input stream containing JavaScript inclusion statements.
71 *
72 * @return JavaScript inclusion statements
73 */
74 public InputStream js() {
Thomas Vachuskad894b5d2015-07-30 11:59:07 -070075 return getStream(resourcePath + JS_HTML);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080076 }
77
78 /**
79 * Returns list of user interface views contributed by this extension.
80 *
81 * @return contributed view descriptors
82 */
83 public List<UiView> views() {
84 return views;
85 }
86
87 /**
88 * Returns input stream containing specified view-specific resource.
89 *
90 * @param viewId view identifier
91 * @param path resource path, relative to the view directory
92 * @return resource input stream
93 */
94 public InputStream resource(String viewId, String path) {
Simon Hunte05cae42015-07-23 17:35:24 -070095 return getStream(VIEW_PREFIX + viewId + SLASH + path);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080096 }
97
Thomas Vachuska3553b302015-03-07 14:49:43 -080098 /**
Simon Hunte05cae42015-07-23 17:35:24 -070099 * Returns message handler factory, if one was defined.
Thomas Vachuska3553b302015-03-07 14:49:43 -0800100 *
Simon Hunte05cae42015-07-23 17:35:24 -0700101 * @return message handler factory
Thomas Vachuska3553b302015-03-07 14:49:43 -0800102 */
103 public UiMessageHandlerFactory messageHandlerFactory() {
104 return messageHandlerFactory;
105 }
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -0700106
Simon Hunte05cae42015-07-23 17:35:24 -0700107 /**
108 * Returns the topology overlay factory, if one was defined.
109 *
110 * @return topology overlay factory
111 */
112 public UiTopoOverlayFactory topoOverlayFactory() {
113 return topoOverlayFactory;
114 }
115
116
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -0700117 // Returns the resource input stream from the specified class-loader.
118 private InputStream getStream(String path) {
119 InputStream stream = classLoader.getResourceAsStream(path);
120 if (stream == null) {
121 log.warn("Unable to find resource {}", path);
122 }
123 return stream;
124 }
125
126
Simon Hunte05cae42015-07-23 17:35:24 -0700127 /**
128 * UI Extension Builder.
129 */
130 public static class Builder {
131 private ClassLoader classLoader;
132
133 private String resourcePath = EMPTY;
134 private List<UiView> views = new ArrayList<>();
135 private UiMessageHandlerFactory messageHandlerFactory = null;
136 private UiTopoOverlayFactory topoOverlayFactory = null;
137
138 /**
139 * Create a builder with the given class loader.
140 * Resource path defaults to "".
141 * Views defaults to an empty list.
142 * Both Message and TopoOverlay factories default to null.
143 *
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700144 * @param cl the class loader
145 * @param views list of views contributed by this extension
Simon Hunte05cae42015-07-23 17:35:24 -0700146 */
147 public Builder(ClassLoader cl, List<UiView> views) {
148 checkNotNull(cl, "Must provide a class loader");
149 checkArgument(views.size() > 0, "Must provide at least one view");
150 this.classLoader = cl;
151 this.views = views;
152 }
153
154 /**
155 * Set the resource path. That is, path to where the CSS and JS
156 * files are located. This value should
157 *
158 * @param path resource path
159 * @return self, for chaining
160 */
161 public Builder resourcePath(String path) {
162 this.resourcePath = path == null ? EMPTY : path + SLASH;
163 return this;
164 }
165
166 /**
167 * Sets the message handler factory for this extension.
168 *
169 * @param mhFactory message handler factory
170 * @return self, for chaining
171 */
172 public Builder messageHandlerFactory(UiMessageHandlerFactory mhFactory) {
173 this.messageHandlerFactory = mhFactory;
174 return this;
175 }
176
177 /**
178 * Sets the topology overlay factory for this extension.
179 *
180 * @param toFactory topology overlay factory
181 * @return self, for chaining
182 */
183 public Builder topoOverlayFactory(UiTopoOverlayFactory toFactory) {
184 this.topoOverlayFactory = toFactory;
185 return this;
186 }
187
188 /**
189 * Builds the UI extension.
190 *
191 * @return UI extension instance
192 */
193 public UiExtension build() {
194 return new UiExtension(classLoader, resourcePath, views,
195 messageHandlerFactory, topoOverlayFactory);
196 }
197
198 }
199
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800200}