Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.ui; |
| 17 | |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Simon Hunt | 7379a3d | 2017-06-20 16:50:39 -0700 | [diff] [blame] | 19 | import org.onosproject.ui.lion.LionBundle; |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 20 | import org.slf4j.Logger; |
| 21 | import org.slf4j.LoggerFactory; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 22 | |
| 23 | import java.io.InputStream; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 24 | import java.util.ArrayList; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 25 | import java.util.List; |
| 26 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 27 | import static com.google.common.base.Preconditions.checkArgument; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 28 | import static com.google.common.base.Preconditions.checkNotNull; |
| 29 | |
| 30 | /** |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 31 | * Immutable representation of a user interface extension. |
| 32 | * <p> |
| 33 | * Note that the {@link Builder} class is used to create a user |
| 34 | * interface extension instance, and that these instances are immutable. |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 35 | */ |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 36 | public final class UiExtension { |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 37 | |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 38 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 39 | |
Thomas Vachuska | 529db0a | 2015-02-23 16:42:14 -0800 | [diff] [blame] | 40 | private static final String VIEW_PREFIX = "app/view/"; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 41 | private static final String EMPTY = ""; |
| 42 | private static final String SLASH = "/"; |
| 43 | private static final String CSS_HTML = "css.html"; |
| 44 | private static final String JS_HTML = "js.html"; |
Thomas Vachuska | 529db0a | 2015-02-23 16:42:14 -0800 | [diff] [blame] | 45 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 46 | private final ClassLoader classLoader; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 47 | private final String resourcePath; |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 48 | private final List<UiView> viewList; |
Simon Hunt | 7379a3d | 2017-06-20 16:50:39 -0700 | [diff] [blame] | 49 | private final List<LionBundle> lionBundles; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 50 | private final UiMessageHandlerFactory messageHandlerFactory; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 51 | private final UiTopoOverlayFactory topoOverlayFactory; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 52 | private final UiTopo2OverlayFactory topo2OverlayFactory; |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 53 | private final UiTopoMapFactory topoMapFactory; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 54 | |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 55 | private boolean isValid = true; |
Sean Condon | 13f42b7 | 2019-05-31 16:17:52 +0100 | [diff] [blame] | 56 | private boolean ui2Valid = true; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 57 | |
| 58 | // private constructor - only the builder calls this |
| 59 | private UiExtension(ClassLoader cl, String path, List<UiView> views, |
Simon Hunt | 7379a3d | 2017-06-20 16:50:39 -0700 | [diff] [blame] | 60 | List<LionBundle> bundles, |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 61 | UiMessageHandlerFactory mhFactory, |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 62 | UiTopoOverlayFactory toFactory, |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 63 | UiTopo2OverlayFactory to2Factory, |
Sean Condon | 13f42b7 | 2019-05-31 16:17:52 +0100 | [diff] [blame] | 64 | UiTopoMapFactory tmFactory, |
| 65 | boolean ui2Vld) { |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 66 | classLoader = cl; |
| 67 | resourcePath = path; |
| 68 | viewList = views; |
Simon Hunt | 7379a3d | 2017-06-20 16:50:39 -0700 | [diff] [blame] | 69 | lionBundles = bundles; |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 70 | messageHandlerFactory = mhFactory; |
| 71 | topoOverlayFactory = toFactory; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 72 | topo2OverlayFactory = to2Factory; |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 73 | topoMapFactory = tmFactory; |
Sean Condon | 13f42b7 | 2019-05-31 16:17:52 +0100 | [diff] [blame] | 74 | ui2Valid = ui2Vld; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * Returns input stream containing CSS inclusion statements. |
| 80 | * |
| 81 | * @return CSS inclusion statements |
| 82 | */ |
| 83 | public InputStream css() { |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 84 | return getStream(resourcePath + CSS_HTML); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns input stream containing JavaScript inclusion statements. |
| 89 | * |
| 90 | * @return JavaScript inclusion statements |
| 91 | */ |
| 92 | public InputStream js() { |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 93 | return getStream(resourcePath + JS_HTML); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns list of user interface views contributed by this extension. |
| 98 | * |
| 99 | * @return contributed view descriptors |
| 100 | */ |
| 101 | public List<UiView> views() { |
Sean Condon | 13f42b7 | 2019-05-31 16:17:52 +0100 | [diff] [blame] | 102 | return (isValid || ui2Valid) ? viewList : ImmutableList.of(); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
Simon Hunt | 7379a3d | 2017-06-20 16:50:39 -0700 | [diff] [blame] | 106 | * Returns the list of localization bundles that this extension is |
| 107 | * contributing. |
| 108 | * |
| 109 | * @return contributed localization bundles |
| 110 | */ |
| 111 | public List<LionBundle> lionBundles() { |
| 112 | return ImmutableList.copyOf(lionBundles); |
| 113 | } |
| 114 | |
| 115 | /** |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 116 | * Returns input stream containing specified view-specific resource. |
| 117 | * |
| 118 | * @param viewId view identifier |
| 119 | * @param path resource path, relative to the view directory |
| 120 | * @return resource input stream |
| 121 | */ |
| 122 | public InputStream resource(String viewId, String path) { |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 123 | return getStream(VIEW_PREFIX + viewId + SLASH + path); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 126 | /** |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 127 | * Returns message handler factory, if one was defined. |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 128 | * |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 129 | * @return message handler factory |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 130 | */ |
| 131 | public UiMessageHandlerFactory messageHandlerFactory() { |
| 132 | return messageHandlerFactory; |
| 133 | } |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 134 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 135 | /** |
| 136 | * Returns the topology overlay factory, if one was defined. |
| 137 | * |
| 138 | * @return topology overlay factory |
| 139 | */ |
| 140 | public UiTopoOverlayFactory topoOverlayFactory() { |
| 141 | return topoOverlayFactory; |
| 142 | } |
| 143 | |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 144 | /** |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 145 | * Returns the topology-2 overlay factory, if one was defined. |
| 146 | * |
| 147 | * @return topology-2 overlay factory |
| 148 | */ |
| 149 | public UiTopo2OverlayFactory topo2OverlayFactory() { |
| 150 | return topo2OverlayFactory; |
| 151 | } |
| 152 | |
| 153 | /** |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 154 | * Returns the topology map factory, if one was defined. |
| 155 | * |
| 156 | * @return topology map factory |
| 157 | */ |
| 158 | public UiTopoMapFactory topoMapFactory() { |
| 159 | return topoMapFactory; |
| 160 | } |
| 161 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 162 | |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 163 | // Returns the resource input stream from the specified class-loader. |
| 164 | private InputStream getStream(String path) { |
| 165 | InputStream stream = classLoader.getResourceAsStream(path); |
| 166 | if (stream == null) { |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 167 | isValid = false; |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 168 | log.warn("Unable to find resource {}", path); |
| 169 | } |
| 170 | return stream; |
| 171 | } |
| 172 | |
| 173 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 174 | /** |
| 175 | * UI Extension Builder. |
| 176 | */ |
| 177 | public static class Builder { |
| 178 | private ClassLoader classLoader; |
| 179 | |
| 180 | private String resourcePath = EMPTY; |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 181 | private List<UiView> viewList = new ArrayList<>(); |
Simon Hunt | 7379a3d | 2017-06-20 16:50:39 -0700 | [diff] [blame] | 182 | private List<LionBundle> lionBundles = new ArrayList<>(); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 183 | private UiMessageHandlerFactory messageHandlerFactory = null; |
| 184 | private UiTopoOverlayFactory topoOverlayFactory = null; |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 185 | private UiTopo2OverlayFactory topo2OverlayFactory = null; |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 186 | private UiTopoMapFactory topoMapFactory = null; |
Sean Condon | 13f42b7 | 2019-05-31 16:17:52 +0100 | [diff] [blame] | 187 | private boolean ui2valid = false; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 188 | |
| 189 | /** |
| 190 | * Create a builder with the given class loader. |
| 191 | * Resource path defaults to "". |
| 192 | * Views defaults to an empty list. |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 193 | * MessageHandler, TopoOverlay, and TopoMap factories default to null. |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 194 | * |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 195 | * @param cl the class loader |
| 196 | * @param views list of views contributed by this extension |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 197 | */ |
| 198 | public Builder(ClassLoader cl, List<UiView> views) { |
| 199 | checkNotNull(cl, "Must provide a class loader"); |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 200 | checkArgument(!views.isEmpty(), "Must provide at least one view"); |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 201 | classLoader = cl; |
| 202 | viewList = views; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /** |
Simon Hunt | 7379a3d | 2017-06-20 16:50:39 -0700 | [diff] [blame] | 206 | * Sets the localization bundles (aka {@code LionBundle}s) that this |
| 207 | * UI extension is contributing. |
| 208 | * |
| 209 | * @param bundles the bundles to register |
| 210 | * @return self, for chaining |
| 211 | */ |
| 212 | public Builder lionBundles(List<LionBundle> bundles) { |
| 213 | checkNotNull(bundles, "Must provide a list"); |
| 214 | lionBundles = bundles; |
| 215 | return this; |
| 216 | } |
| 217 | |
| 218 | /** |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 219 | * Set the resource path. That is, the path to where the CSS and JS |
| 220 | * files are located. |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 221 | * |
| 222 | * @param path resource path |
| 223 | * @return self, for chaining |
| 224 | */ |
| 225 | public Builder resourcePath(String path) { |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 226 | resourcePath = path == null ? EMPTY : path + SLASH; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 227 | return this; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Sets the message handler factory for this extension. |
| 232 | * |
| 233 | * @param mhFactory message handler factory |
| 234 | * @return self, for chaining |
| 235 | */ |
| 236 | public Builder messageHandlerFactory(UiMessageHandlerFactory mhFactory) { |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 237 | messageHandlerFactory = mhFactory; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 238 | return this; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Sets the topology overlay factory for this extension. |
| 243 | * |
| 244 | * @param toFactory topology overlay factory |
| 245 | * @return self, for chaining |
| 246 | */ |
| 247 | public Builder topoOverlayFactory(UiTopoOverlayFactory toFactory) { |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 248 | topoOverlayFactory = toFactory; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 249 | return this; |
| 250 | } |
| 251 | |
| 252 | /** |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 253 | * Sets the topology-2 overlay factory for this extension. |
| 254 | * |
| 255 | * @param to2Factory topology-2 overlay factory |
| 256 | * @return self, for chaining |
| 257 | */ |
| 258 | public Builder topo2OverlayFactory(UiTopo2OverlayFactory to2Factory) { |
| 259 | topo2OverlayFactory = to2Factory; |
| 260 | return this; |
| 261 | } |
| 262 | |
| 263 | /** |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 264 | * Sets the topology map factory for this extension. |
| 265 | * |
| 266 | * @param tmFactory topology map factory |
| 267 | * @return self, for chaining |
| 268 | */ |
| 269 | public Builder topoMapFactory(UiTopoMapFactory tmFactory) { |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 270 | topoMapFactory = tmFactory; |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 271 | return this; |
| 272 | } |
| 273 | |
| 274 | /** |
Sean Condon | 13f42b7 | 2019-05-31 16:17:52 +0100 | [diff] [blame] | 275 | * Marks this as ui2valid for Ui2. |
| 276 | * This is because Ui2 does not include the same layout of embedded html, js and css |
| 277 | * |
| 278 | * @return self, for chaining |
| 279 | */ |
| 280 | public Builder ui2() { |
| 281 | ui2valid = true; |
| 282 | return this; |
| 283 | } |
| 284 | |
| 285 | /** |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 286 | * Builds the user interface extension. |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 287 | * |
| 288 | * @return UI extension instance |
| 289 | */ |
| 290 | public UiExtension build() { |
Simon Hunt | 8add9ee | 2016-09-20 17:05:07 -0700 | [diff] [blame] | 291 | return new UiExtension(classLoader, resourcePath, viewList, |
Simon Hunt | 7379a3d | 2017-06-20 16:50:39 -0700 | [diff] [blame] | 292 | lionBundles, |
Simon Hunt | 22c35df | 2017-04-26 17:28:42 -0700 | [diff] [blame] | 293 | messageHandlerFactory, topoOverlayFactory, |
Sean Condon | 13f42b7 | 2019-05-31 16:17:52 +0100 | [diff] [blame] | 294 | topo2OverlayFactory, topoMapFactory, ui2valid); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 295 | } |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 296 | } |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 297 | } |