blob: c3108584b665a7a0c833b4d65409ea9cdde6ad47 [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() {
Simon Hunte05cae42015-07-23 17:35:24 -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 *
144 * @param cl the classloader
145 */
146 public Builder(ClassLoader cl, List<UiView> views) {
147 checkNotNull(cl, "Must provide a class loader");
148 checkArgument(views.size() > 0, "Must provide at least one view");
149 this.classLoader = cl;
150 this.views = views;
151 }
152
153 /**
154 * Set the resource path. That is, path to where the CSS and JS
155 * files are located. This value should
156 *
157 * @param path resource path
158 * @return self, for chaining
159 */
160 public Builder resourcePath(String path) {
161 this.resourcePath = path == null ? EMPTY : path + SLASH;
162 return this;
163 }
164
165 /**
166 * Sets the message handler factory for this extension.
167 *
168 * @param mhFactory message handler factory
169 * @return self, for chaining
170 */
171 public Builder messageHandlerFactory(UiMessageHandlerFactory mhFactory) {
172 this.messageHandlerFactory = mhFactory;
173 return this;
174 }
175
176 /**
177 * Sets the topology overlay factory for this extension.
178 *
179 * @param toFactory topology overlay factory
180 * @return self, for chaining
181 */
182 public Builder topoOverlayFactory(UiTopoOverlayFactory toFactory) {
183 this.topoOverlayFactory = toFactory;
184 return this;
185 }
186
187 /**
188 * Builds the UI extension.
189 *
190 * @return UI extension instance
191 */
192 public UiExtension build() {
193 return new UiExtension(classLoader, resourcePath, views,
194 messageHandlerFactory, topoOverlayFactory);
195 }
196
197 }
198
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800199}