Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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; |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 19 | import org.slf4j.Logger; |
| 20 | import org.slf4j.LoggerFactory; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 21 | |
| 22 | import java.io.InputStream; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 23 | import java.util.ArrayList; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 24 | import java.util.List; |
| 25 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 26 | import static com.google.common.base.Preconditions.checkArgument; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | |
| 29 | /** |
| 30 | * User interface extension. |
| 31 | */ |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 32 | public final class UiExtension { |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 33 | |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 34 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 35 | |
Thomas Vachuska | 529db0a | 2015-02-23 16:42:14 -0800 | [diff] [blame] | 36 | private static final String VIEW_PREFIX = "app/view/"; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 37 | 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 Vachuska | 529db0a | 2015-02-23 16:42:14 -0800 | [diff] [blame] | 41 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 42 | private final ClassLoader classLoader; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 43 | private final String resourcePath; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 44 | private final List<UiView> views; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 45 | private final UiMessageHandlerFactory messageHandlerFactory; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 46 | private final UiTopoOverlayFactory topoOverlayFactory; |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 47 | private final UiTopoMapFactory topoMapFactory; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 48 | |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 49 | private boolean isValid = true; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 50 | |
| 51 | // private constructor - only the builder calls this |
| 52 | private UiExtension(ClassLoader cl, String path, List<UiView> views, |
| 53 | UiMessageHandlerFactory mhFactory, |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 54 | UiTopoOverlayFactory toFactory, |
| 55 | UiTopoMapFactory tmFactory) { |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 56 | this.classLoader = cl; |
| 57 | this.resourcePath = path; |
| 58 | this.views = views; |
| 59 | this.messageHandlerFactory = mhFactory; |
| 60 | this.topoOverlayFactory = toFactory; |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 61 | this.topoMapFactory = tmFactory; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Returns input stream containing CSS inclusion statements. |
| 67 | * |
| 68 | * @return CSS inclusion statements |
| 69 | */ |
| 70 | public InputStream css() { |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 71 | return getStream(resourcePath + CSS_HTML); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns input stream containing JavaScript inclusion statements. |
| 76 | * |
| 77 | * @return JavaScript inclusion statements |
| 78 | */ |
| 79 | public InputStream js() { |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 80 | return getStream(resourcePath + JS_HTML); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns list of user interface views contributed by this extension. |
| 85 | * |
| 86 | * @return contributed view descriptors |
| 87 | */ |
| 88 | public List<UiView> views() { |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 89 | return isValid ? views : ImmutableList.of(); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns input stream containing specified view-specific resource. |
| 94 | * |
| 95 | * @param viewId view identifier |
| 96 | * @param path resource path, relative to the view directory |
| 97 | * @return resource input stream |
| 98 | */ |
| 99 | public InputStream resource(String viewId, String path) { |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 100 | return getStream(VIEW_PREFIX + viewId + SLASH + path); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 103 | /** |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 104 | * Returns message handler factory, if one was defined. |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 105 | * |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 106 | * @return message handler factory |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 107 | */ |
| 108 | public UiMessageHandlerFactory messageHandlerFactory() { |
| 109 | return messageHandlerFactory; |
| 110 | } |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 111 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 112 | /** |
| 113 | * Returns the topology overlay factory, if one was defined. |
| 114 | * |
| 115 | * @return topology overlay factory |
| 116 | */ |
| 117 | public UiTopoOverlayFactory topoOverlayFactory() { |
| 118 | return topoOverlayFactory; |
| 119 | } |
| 120 | |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 121 | /** |
| 122 | * Returns the topology map factory, if one was defined. |
| 123 | * |
| 124 | * @return topology map factory |
| 125 | */ |
| 126 | public UiTopoMapFactory topoMapFactory() { |
| 127 | return topoMapFactory; |
| 128 | } |
| 129 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 130 | |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 131 | // Returns the resource input stream from the specified class-loader. |
| 132 | private InputStream getStream(String path) { |
| 133 | InputStream stream = classLoader.getResourceAsStream(path); |
| 134 | if (stream == null) { |
Thomas Vachuska | c4178cc | 2015-12-10 11:43:32 -0800 | [diff] [blame] | 135 | isValid = false; |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 136 | log.warn("Unable to find resource {}", path); |
| 137 | } |
| 138 | return stream; |
| 139 | } |
| 140 | |
| 141 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 142 | /** |
| 143 | * UI Extension Builder. |
| 144 | */ |
| 145 | public static class Builder { |
| 146 | private ClassLoader classLoader; |
| 147 | |
| 148 | private String resourcePath = EMPTY; |
| 149 | private List<UiView> views = new ArrayList<>(); |
| 150 | private UiMessageHandlerFactory messageHandlerFactory = null; |
| 151 | private UiTopoOverlayFactory topoOverlayFactory = null; |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 152 | private UiTopoMapFactory topoMapFactory = null; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 153 | |
| 154 | /** |
| 155 | * Create a builder with the given class loader. |
| 156 | * Resource path defaults to "". |
| 157 | * Views defaults to an empty list. |
| 158 | * Both Message and TopoOverlay factories default to null. |
| 159 | * |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 160 | * @param cl the class loader |
| 161 | * @param views list of views contributed by this extension |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 162 | */ |
| 163 | public Builder(ClassLoader cl, List<UiView> views) { |
| 164 | checkNotNull(cl, "Must provide a class loader"); |
| 165 | checkArgument(views.size() > 0, "Must provide at least one view"); |
| 166 | this.classLoader = cl; |
| 167 | this.views = views; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Set the resource path. That is, path to where the CSS and JS |
| 172 | * files are located. This value should |
| 173 | * |
| 174 | * @param path resource path |
| 175 | * @return self, for chaining |
| 176 | */ |
| 177 | public Builder resourcePath(String path) { |
| 178 | this.resourcePath = path == null ? EMPTY : path + SLASH; |
| 179 | return this; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Sets the message handler factory for this extension. |
| 184 | * |
| 185 | * @param mhFactory message handler factory |
| 186 | * @return self, for chaining |
| 187 | */ |
| 188 | public Builder messageHandlerFactory(UiMessageHandlerFactory mhFactory) { |
| 189 | this.messageHandlerFactory = mhFactory; |
| 190 | return this; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Sets the topology overlay factory for this extension. |
| 195 | * |
| 196 | * @param toFactory topology overlay factory |
| 197 | * @return self, for chaining |
| 198 | */ |
| 199 | public Builder topoOverlayFactory(UiTopoOverlayFactory toFactory) { |
| 200 | this.topoOverlayFactory = toFactory; |
| 201 | return this; |
| 202 | } |
| 203 | |
| 204 | /** |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 205 | * Sets the topology map factory for this extension. |
| 206 | * |
| 207 | * @param tmFactory topology map factory |
| 208 | * @return self, for chaining |
| 209 | */ |
| 210 | public Builder topoMapFactory(UiTopoMapFactory tmFactory) { |
| 211 | this.topoMapFactory = tmFactory; |
| 212 | return this; |
| 213 | } |
| 214 | |
| 215 | /** |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 216 | * Builds the UI extension. |
| 217 | * |
| 218 | * @return UI extension instance |
| 219 | */ |
| 220 | public UiExtension build() { |
| 221 | return new UiExtension(classLoader, resourcePath, views, |
Steven Burrows | 3a9a644 | 2016-05-05 15:31:16 +0100 | [diff] [blame] | 222 | messageHandlerFactory, topoOverlayFactory, |
| 223 | topoMapFactory); |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | } |
| 227 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 228 | } |