Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.ui; |
| 17 | |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 18 | import org.slf4j.Logger; |
| 19 | import org.slf4j.LoggerFactory; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 20 | |
| 21 | import java.io.InputStream; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 22 | import java.util.ArrayList; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 23 | import java.util.List; |
| 24 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 25 | import static com.google.common.base.Preconditions.checkArgument; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | |
| 28 | /** |
| 29 | * User interface extension. |
| 30 | */ |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 31 | public final class UiExtension { |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 32 | |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 33 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 34 | |
Thomas Vachuska | 529db0a | 2015-02-23 16:42:14 -0800 | [diff] [blame] | 35 | private static final String VIEW_PREFIX = "app/view/"; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 36 | 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 Vachuska | 529db0a | 2015-02-23 16:42:14 -0800 | [diff] [blame] | 40 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 41 | private final ClassLoader classLoader; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 42 | private final String resourcePath; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 43 | private final List<UiView> views; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 44 | private final UiMessageHandlerFactory messageHandlerFactory; |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 45 | private final UiTopoOverlayFactory topoOverlayFactory; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 46 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 47 | |
| 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 Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Returns input stream containing CSS inclusion statements. |
| 62 | * |
| 63 | * @return CSS inclusion statements |
| 64 | */ |
| 65 | public InputStream css() { |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 66 | return getStream(resourcePath + CSS_HTML); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns input stream containing JavaScript inclusion statements. |
| 71 | * |
| 72 | * @return JavaScript inclusion statements |
| 73 | */ |
| 74 | public InputStream js() { |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 75 | return getStream(resourcePath + JS_HTML); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 76 | } |
| 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 Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 95 | return getStream(VIEW_PREFIX + viewId + SLASH + path); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 98 | /** |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 99 | * Returns message handler factory, if one was defined. |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 100 | * |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 101 | * @return message handler factory |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 102 | */ |
| 103 | public UiMessageHandlerFactory messageHandlerFactory() { |
| 104 | return messageHandlerFactory; |
| 105 | } |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 106 | |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 107 | /** |
| 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 Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 117 | // 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 Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 127 | /** |
| 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 | * |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 144 | * @param cl the class loader |
| 145 | * @param views list of views contributed by this extension |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 146 | */ |
| 147 | public Builder(ClassLoader cl, List<UiView> views) { |
| 148 | checkNotNull(cl, "Must provide a class loader"); |
| 149 | checkArgument(views.size() > 0, "Must provide at least one view"); |
| 150 | this.classLoader = cl; |
| 151 | this.views = views; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Set the resource path. That is, path to where the CSS and JS |
| 156 | * files are located. This value should |
| 157 | * |
| 158 | * @param path resource path |
| 159 | * @return self, for chaining |
| 160 | */ |
| 161 | public Builder resourcePath(String path) { |
| 162 | this.resourcePath = path == null ? EMPTY : path + SLASH; |
| 163 | return this; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Sets the message handler factory for this extension. |
| 168 | * |
| 169 | * @param mhFactory message handler factory |
| 170 | * @return self, for chaining |
| 171 | */ |
| 172 | public Builder messageHandlerFactory(UiMessageHandlerFactory mhFactory) { |
| 173 | this.messageHandlerFactory = mhFactory; |
| 174 | return this; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Sets the topology overlay factory for this extension. |
| 179 | * |
| 180 | * @param toFactory topology overlay factory |
| 181 | * @return self, for chaining |
| 182 | */ |
| 183 | public Builder topoOverlayFactory(UiTopoOverlayFactory toFactory) { |
| 184 | this.topoOverlayFactory = toFactory; |
| 185 | return this; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Builds the UI extension. |
| 190 | * |
| 191 | * @return UI extension instance |
| 192 | */ |
| 193 | public UiExtension build() { |
| 194 | return new UiExtension(classLoader, resourcePath, views, |
| 195 | messageHandlerFactory, topoOverlayFactory); |
| 196 | } |
| 197 | |
| 198 | } |
| 199 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 200 | } |