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 | |
| 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; |
| 23 | import java.util.List; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | |
| 27 | /** |
| 28 | * User interface extension. |
| 29 | */ |
| 30 | public class UiExtension { |
| 31 | |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 32 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 33 | |
Thomas Vachuska | 529db0a | 2015-02-23 16:42:14 -0800 | [diff] [blame] | 34 | private static final String VIEW_PREFIX = "app/view/"; |
| 35 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 36 | private final String prefix; |
| 37 | private final ClassLoader classLoader; |
| 38 | private final List<UiView> views; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 39 | private final UiMessageHandlerFactory messageHandlerFactory; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Creates a user interface extension for loading CSS and JS injections |
| 43 | * from {@code css.html} and {@code js.html} resources, respectively. |
| 44 | * |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 45 | * @param views list of contributed views |
| 46 | * @param messageHandlerFactory optional message handler factory |
| 47 | * @param classLoader class-loader for user interface resources |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 48 | */ |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 49 | public UiExtension(List<UiView> views, |
| 50 | UiMessageHandlerFactory messageHandlerFactory, |
| 51 | ClassLoader classLoader) { |
| 52 | this(views, messageHandlerFactory, null, classLoader); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Creates a user interface extension using custom resource prefix. It |
| 57 | * loads CSS and JS injections from {@code path/css.html} and |
| 58 | * {@code prefix/js.html} resources, respectively. |
| 59 | * |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 60 | * @param views list of user interface views |
| 61 | * @param messageHandlerFactory optional message handler factory |
| 62 | * @param path resource path prefix |
| 63 | * @param classLoader class-loader for user interface resources |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 64 | */ |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 65 | public UiExtension(List<UiView> views, |
| 66 | UiMessageHandlerFactory messageHandlerFactory, |
| 67 | String path, ClassLoader classLoader) { |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 68 | this.views = checkNotNull(ImmutableList.copyOf(views), "Views cannot be null"); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 69 | this.messageHandlerFactory = messageHandlerFactory; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 70 | this.prefix = path != null ? (path + "/") : ""; |
| 71 | this.classLoader = checkNotNull(classLoader, "Class loader must be specified"); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns input stream containing CSS inclusion statements. |
| 76 | * |
| 77 | * @return CSS inclusion statements |
| 78 | */ |
| 79 | public InputStream css() { |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 80 | return getStream(prefix + "css.html"); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns input stream containing JavaScript inclusion statements. |
| 85 | * |
| 86 | * @return JavaScript inclusion statements |
| 87 | */ |
| 88 | public InputStream js() { |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 89 | return getStream(prefix + "js.html"); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns list of user interface views contributed by this extension. |
| 94 | * |
| 95 | * @return contributed view descriptors |
| 96 | */ |
| 97 | public List<UiView> views() { |
| 98 | return views; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns input stream containing specified view-specific resource. |
| 103 | * |
| 104 | * @param viewId view identifier |
| 105 | * @param path resource path, relative to the view directory |
| 106 | * @return resource input stream |
| 107 | */ |
| 108 | public InputStream resource(String viewId, String path) { |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 109 | return getStream(VIEW_PREFIX + viewId + "/" + path); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 112 | /** |
| 113 | * Returns message handler factory. |
| 114 | * |
| 115 | * @return message handlers |
| 116 | */ |
| 117 | public UiMessageHandlerFactory messageHandlerFactory() { |
| 118 | return messageHandlerFactory; |
| 119 | } |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 120 | |
| 121 | // Returns the resource input stream from the specified class-loader. |
| 122 | private InputStream getStream(String path) { |
| 123 | InputStream stream = classLoader.getResourceAsStream(path); |
| 124 | if (stream == null) { |
| 125 | log.warn("Unable to find resource {}", path); |
| 126 | } |
| 127 | return stream; |
| 128 | } |
| 129 | |
| 130 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 131 | } |