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