blob: 06c6e3bbfe146589a05975934833a57bd97638a1 [file] [log] [blame]
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -08003 *
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 Vachuskac4178cc2015-12-10 11:43:32 -080018import com.google.common.collect.ImmutableList;
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -070019import org.slf4j.Logger;
20import org.slf4j.LoggerFactory;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080021
22import java.io.InputStream;
Simon Hunte05cae42015-07-23 17:35:24 -070023import java.util.ArrayList;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080024import java.util.List;
25
Simon Hunte05cae42015-07-23 17:35:24 -070026import static com.google.common.base.Preconditions.checkArgument;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080027import static com.google.common.base.Preconditions.checkNotNull;
28
29/**
30 * User interface extension.
31 */
Simon Hunte05cae42015-07-23 17:35:24 -070032public final class UiExtension {
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080033
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -070034 private final Logger log = LoggerFactory.getLogger(getClass());
35
Thomas Vachuska529db0a2015-02-23 16:42:14 -080036 private static final String VIEW_PREFIX = "app/view/";
Simon Hunte05cae42015-07-23 17:35:24 -070037 private static final String EMPTY = "";
38 private static final String SLASH = "/";
39 private static final String CSS_HTML = "css.html";
40 private static final String JS_HTML = "js.html";
Thomas Vachuska529db0a2015-02-23 16:42:14 -080041
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080042 private final ClassLoader classLoader;
Simon Hunte05cae42015-07-23 17:35:24 -070043 private final String resourcePath;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080044 private final List<UiView> views;
Thomas Vachuska3553b302015-03-07 14:49:43 -080045 private final UiMessageHandlerFactory messageHandlerFactory;
Simon Hunte05cae42015-07-23 17:35:24 -070046 private final UiTopoOverlayFactory topoOverlayFactory;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080047
Thomas Vachuskac4178cc2015-12-10 11:43:32 -080048 private boolean isValid = true;
Simon Hunte05cae42015-07-23 17:35:24 -070049
50 // private constructor - only the builder calls this
51 private UiExtension(ClassLoader cl, String path, List<UiView> views,
52 UiMessageHandlerFactory mhFactory,
53 UiTopoOverlayFactory toFactory) {
54 this.classLoader = cl;
55 this.resourcePath = path;
56 this.views = views;
57 this.messageHandlerFactory = mhFactory;
58 this.topoOverlayFactory = toFactory;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080059 }
60
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080061
62 /**
63 * Returns input stream containing CSS inclusion statements.
64 *
65 * @return CSS inclusion statements
66 */
67 public InputStream css() {
Simon Hunte05cae42015-07-23 17:35:24 -070068 return getStream(resourcePath + CSS_HTML);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080069 }
70
71 /**
72 * Returns input stream containing JavaScript inclusion statements.
73 *
74 * @return JavaScript inclusion statements
75 */
76 public InputStream js() {
Thomas Vachuskad894b5d2015-07-30 11:59:07 -070077 return getStream(resourcePath + JS_HTML);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080078 }
79
80 /**
81 * Returns list of user interface views contributed by this extension.
82 *
83 * @return contributed view descriptors
84 */
85 public List<UiView> views() {
Thomas Vachuskac4178cc2015-12-10 11:43:32 -080086 return isValid ? views : ImmutableList.of();
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080087 }
88
89 /**
90 * Returns input stream containing specified view-specific resource.
91 *
92 * @param viewId view identifier
93 * @param path resource path, relative to the view directory
94 * @return resource input stream
95 */
96 public InputStream resource(String viewId, String path) {
Simon Hunte05cae42015-07-23 17:35:24 -070097 return getStream(VIEW_PREFIX + viewId + SLASH + path);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080098 }
99
Thomas Vachuska3553b302015-03-07 14:49:43 -0800100 /**
Simon Hunte05cae42015-07-23 17:35:24 -0700101 * Returns message handler factory, if one was defined.
Thomas Vachuska3553b302015-03-07 14:49:43 -0800102 *
Simon Hunte05cae42015-07-23 17:35:24 -0700103 * @return message handler factory
Thomas Vachuska3553b302015-03-07 14:49:43 -0800104 */
105 public UiMessageHandlerFactory messageHandlerFactory() {
106 return messageHandlerFactory;
107 }
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -0700108
Simon Hunte05cae42015-07-23 17:35:24 -0700109 /**
110 * Returns the topology overlay factory, if one was defined.
111 *
112 * @return topology overlay factory
113 */
114 public UiTopoOverlayFactory topoOverlayFactory() {
115 return topoOverlayFactory;
116 }
117
118
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -0700119 // Returns the resource input stream from the specified class-loader.
120 private InputStream getStream(String path) {
121 InputStream stream = classLoader.getResourceAsStream(path);
122 if (stream == null) {
Thomas Vachuskac4178cc2015-12-10 11:43:32 -0800123 isValid = false;
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -0700124 log.warn("Unable to find resource {}", path);
125 }
126 return stream;
127 }
128
129
Simon Hunte05cae42015-07-23 17:35:24 -0700130 /**
131 * UI Extension Builder.
132 */
133 public static class Builder {
134 private ClassLoader classLoader;
135
136 private String resourcePath = EMPTY;
137 private List<UiView> views = new ArrayList<>();
138 private UiMessageHandlerFactory messageHandlerFactory = null;
139 private UiTopoOverlayFactory topoOverlayFactory = null;
140
141 /**
142 * Create a builder with the given class loader.
143 * Resource path defaults to "".
144 * Views defaults to an empty list.
145 * Both Message and TopoOverlay factories default to null.
146 *
Thomas Vachuskad894b5d2015-07-30 11:59:07 -0700147 * @param cl the class loader
148 * @param views list of views contributed by this extension
Simon Hunte05cae42015-07-23 17:35:24 -0700149 */
150 public Builder(ClassLoader cl, List<UiView> views) {
151 checkNotNull(cl, "Must provide a class loader");
152 checkArgument(views.size() > 0, "Must provide at least one view");
153 this.classLoader = cl;
154 this.views = views;
155 }
156
157 /**
158 * Set the resource path. That is, path to where the CSS and JS
159 * files are located. This value should
160 *
161 * @param path resource path
162 * @return self, for chaining
163 */
164 public Builder resourcePath(String path) {
165 this.resourcePath = path == null ? EMPTY : path + SLASH;
166 return this;
167 }
168
169 /**
170 * Sets the message handler factory for this extension.
171 *
172 * @param mhFactory message handler factory
173 * @return self, for chaining
174 */
175 public Builder messageHandlerFactory(UiMessageHandlerFactory mhFactory) {
176 this.messageHandlerFactory = mhFactory;
177 return this;
178 }
179
180 /**
181 * Sets the topology overlay factory for this extension.
182 *
183 * @param toFactory topology overlay factory
184 * @return self, for chaining
185 */
186 public Builder topoOverlayFactory(UiTopoOverlayFactory toFactory) {
187 this.topoOverlayFactory = toFactory;
188 return this;
189 }
190
191 /**
192 * Builds the UI extension.
193 *
194 * @return UI extension instance
195 */
196 public UiExtension build() {
197 return new UiExtension(classLoader, resourcePath, views,
198 messageHandlerFactory, topoOverlayFactory);
199 }
200
201 }
202
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800203}