Commit 6fd14ddf authored by Ate Douma's avatar Ate Douma

TRIVIAL cleanup master

parent 3f77412b
* text=auto !eol
/LICENSE -text
/NOTICE -text
src/main/java/org/onehippo/cms7/services/autoreload/AutoReloadServiceModule.java -text
src/main/resources/autoreload-service-module.xml -text
src/main/resources/hippoecm-extension.xml -text
/*.iml
/.idea
/target
This diff is collapsed.
Hippo CMS7 Services - autoreload
Copyright 2014-2018 Hippo B.V. (http://www.onehippo.com)
This product includes software developed by:
Hippo B.V., Amsterdam, The Netherlands (http://www.onehippo.com/);
The Apache Software Foundation (http://www.apache.org/).
NOTICE: Only our own original work is licensed under the terms of the
Apache License Version 2.0. The licenses of some libraries might impose
different redistribution or general licensing terms than those stated in the
Apache License. Users and redistributors are hereby requested to verify these
conditions and agree upon them.
# This is not the branch you're looking for...
BloomReach only provides the git trees for the release tags of Hippo CMS, as explained on https://www.onehippo.org/about/open-source-release-policy.html
To checkout the code for a specific release tag, after cloning this repository, use the following:
## to show the available tags
git tag
## to checkout a specific tag
git checkout <tag name>
## to modify a project
If you want to make modifications to a project, for example to create a patch, create a new fork branch from the specific tag like this:
git checkout -b forked-<tag name> <tag name>
For the latter, also see the **Build from Source** documentation at https://www.onehippo.org/library/development/build-hippo-cms-from-scratch.html
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2014-2018 Hippo B.V. (http://www.onehippo.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.onehippo.cms7</groupId>
<artifactId>hippo-cms7-project</artifactId>
<version>29.4-SNAPSHOT</version>
</parent>
<name>Hippo CMS7 Services - autoreload</name>
<artifactId>hippo-services-autoreload</artifactId>
<version>4.3.0-SNAPSHOT</version>
<inceptionYear>2014</inceptionYear>
<properties>
<!-- use root project name for all project modules NOTICE files, should be the same as in the root NOTICE file -->
<notice.project.name>Hippo CMS7 Services - autoreload</notice.project.name>
<!-- runtime dependencies -->
<hippo.repository.version>5.3.0-SNAPSHOT</hippo.repository.version>
<hippo.services.version>4.3.0-SNAPSHOT</hippo.services.version>
<javax.websocket-api.version>1.1</javax.websocket-api.version>
<!-- test dependencies -->
<commons-io.version>2.5</commons-io.version>
<commons-lang.version>2.6</commons-lang.version>
<easymock.version>3.0</easymock.version>
<jslint.plugin.version>1.0.1</jslint.plugin.version>
<junit.version>4.12</junit.version>
</properties>
<scm>
<connection>scm:git:https://code.onehippo.org/cms-community/hippo-services-autoreload.git</connection>
<developerConnection>scm:git:git@code.onehippo.org:cms-community/hippo-services-autoreload.git</developerConnection>
<url>https://code.onehippo.org/cms-community/hippo-services-autoreload</url>
</scm>
<repositories>
<repository>
<id>hippo</id>
<name>Hippo Maven 2</name>
<url>https://maven.onehippo.com/maven2/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
</repository>
</repositories>
<dependencies>
<!--
N.B. since the auto-reload service is deployed in Tomcat's shared/lib directory,
only dependencies available in Tomcat's shared/lib directory can be used here.
-->
<dependency>
<groupId>org.onehippo.cms7</groupId>
<artifactId>hippo-repository-api</artifactId>
<version>${hippo.repository.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.onehippo.cms7</groupId>
<artifactId>hippo-services</artifactId>
<version>${hippo.services.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>${javax.websocket-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>${easymock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commons-lang.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.onehippo.cms7</groupId>
<artifactId>hippo-repository-testutils</artifactId>
<version>${hippo.repository.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jslint-maven-plugin</artifactId>
<version>${jslint.plugin.version}</version>
<executions>
<execution>
<goals>
<goal>jslint</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceJsFolder>${basedir}/src/main/resources</sourceJsFolder>
<predefinedVars>
console,
reconnect, <!-- ignore valid circular function dependency: connect -> onClose -> reconnect -->
window
</predefinedVars>
</configuration>
</plugin>
</plugins>
</build>
</project>
/*
* Copyright 2014 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onehippo.cms7.services.autoreload;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Loads the auto-reload JavaScript snippet.
*/
class AutoReloadScriptLoader {
private static final String AUTO_RELOAD_SCRIPT = "autoreload.js";
private static final int READ_BUF_SIZE = 1024;
private static final Logger log = LoggerFactory.getLogger(AutoReloadScriptLoader.class);
/**
* @return the auto-reload JavaScript snippet, or null if the snippet could not be loaded.
*/
String getJavaScript() {
InputStream input = null;
try {
input = AutoReloadScriptLoader.class.getResourceAsStream(AUTO_RELOAD_SCRIPT);
if (input == null) {
log.error("Could not locate " + AUTO_RELOAD_SCRIPT);
} else {
return readString(input, "UTF-8");
}
} catch (IOException e) {
log.warn("Error while loading " + AUTO_RELOAD_SCRIPT, e);
} finally {
closeQuietly(input);
}
log.warn("Script {} could not be loaded", AUTO_RELOAD_SCRIPT);
return null;
}
private String readString(final InputStream in, final String encoding) throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final byte[] buffer = new byte[READ_BUF_SIZE];
int length;
while ((length = in.read(buffer)) != -1) {
out.write(buffer, 0, length);
}
return new String(out.toByteArray(), encoding);
}
private void closeQuietly(final InputStream input) {
try {
input.close();
} catch (IOException e) {
log.debug("Ignored exception while closing input stream", e);
}
}
}
/*
* Copyright 2014 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onehippo.cms7.services.autoreload;
import java.io.IOException;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ServerEndpoint(value = "/autoreload", configurator = AutoReloadServerConfigurator.class)
public class AutoReloadServer {
private static final String RELOAD_PAGE_MESSAGE_JSON = "{\"command\":\"reloadPage\"}";
static Logger log = LoggerFactory.getLogger(AutoReloadServer.class);
private static AutoReloadServer instance = null;
private final Queue<Session> sessions;
private AutoReloadServer() {
sessions = new ConcurrentLinkedQueue();
log.info("auto-reload server created");
}
static synchronized AutoReloadServer getInstance() {
if (instance == null) {
instance = new AutoReloadServer();
}
return instance;
}
@OnOpen
public void onOpen(final Session session) {
sessions.add(session);
log.info("auto-reload connection '{}' opened, #connections = {}", session.getId(), sessions.size());
}
@OnMessage
public void onMessage(final Session session, final String msg) {
log.warn("closing auto-reload connection '{}', unexpected message: '{}'", session.getId(), msg);
closeQuitely(session);
}
private void closeQuitely(final Session session) {
try {
session.close();
} catch (IOException e) {
log.debug("Error while closing auto-reload connection:", e);
}
}
@OnClose
public void onClose(final Session session) {
sessions.remove(session);
log.info("auto-reload connection '{}' closed, #connections = {}", session.getId(), sessions.size());
}
@OnError
public void onError(final Throwable t) {
log.debug("ignoring auto-reload websocket error:", t);
}
void broadcastPageReload() {
log.debug("broadcasting page reload");
broadcast(RELOAD_PAGE_MESSAGE_JSON);
}
private void broadcast(final String message) {
for (Session session : sessions) {
session.getAsyncRemote().sendText(message);
}
}
void clearSessions() {
sessions.clear();
}
}
\ No newline at end of file
/*
* Copyright 2014 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onehippo.cms7.services.autoreload;
import javax.websocket.server.ServerEndpointConfig;
/**
* Ensures that the same instance of the {@link org.onehippo.cms7.services.autoreload.AutoReloadServer}
* is used for each web socket connection.
*/
public class AutoReloadServerConfigurator extends ServerEndpointConfig.Configurator {
@Override
public <T> T getEndpointInstance(final Class<T> endpointClass) throws InstantiationException {
if (endpointClass.equals(AutoReloadServer.class)) {
return (T) AutoReloadServer.getInstance();
}
throw new InstantiationException("Cannot create instance of " + endpointClass);
}
}
/*
* Copyright 2014 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onehippo.cms7.services.autoreload;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import org.hippoecm.repository.util.JcrUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* JCR-based, thread-safe, reconfigurable configuration of the auto-reload service.
*/
class AutoReloadServiceConfig {
private static final String CONFIG_ENABLED = "enabled";
private static final boolean DEFAULT_ENABLED = true;
private static final Logger log = LoggerFactory.getLogger(AutoReloadServiceConfig.class);
private AtomicBoolean isEnabled = new AtomicBoolean(DEFAULT_ENABLED);
void reconfigure(final Node config) throws RepositoryException {
isEnabled.set(JcrUtils.getBooleanProperty(config, CONFIG_ENABLED, DEFAULT_ENABLED));
log.info("Reconfigured auto-reload service: enabled=" + isEnabled);
}
public boolean isEnabled() {
return isEnabled.get();
}
}
/*
* Copyright 2014 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onehippo.cms7.services.autoreload;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Auto-reload service based on web sockets.
*/
class AutoReloadServiceImpl implements AutoReloadService {
static final String DEFAULT_CONTEXT_PATH_VALUE = "/site";
private final AutoReloadServiceConfig config;
private final AutoReloadServer autoReloadServer;
private final AtomicBoolean enabled;
private final String cachedJavaScript;
AutoReloadServiceImpl(final AutoReloadServiceConfig config, final AutoReloadScriptLoader scriptLoader, final AutoReloadServer autoReloadServer) {
this.config = config;
this.autoReloadServer = autoReloadServer;
enabled = new AtomicBoolean(config.isEnabled());
cachedJavaScript = scriptLoader.getJavaScript();
}
@Override
public boolean isEnabled() {
return cachedJavaScript != null && enabled.get() && config.isEnabled();
}
@Override
public void setEnabled(final boolean isEnabled) {
enabled.set(isEnabled);
}
@Override
public String getJavaScript(final String contextPath) {
if (contextPath == null) {
throw new IllegalArgumentException("context path cannot be null");
}
if (!contextPath.isEmpty() && (!contextPath.startsWith("/") || contextPath.endsWith("/"))) {
throw new IllegalArgumentException("illegal context path: '" + contextPath + "'");
}
if (cachedJavaScript != null) {
return cachedJavaScript.replace(DEFAULT_CONTEXT_PATH_VALUE, contextPath);
}
return null;
}
@Override
public void broadcastPageReload() {
if (isEnabled()) {
autoReloadServer.broadcastPageReload();
}
}
}
/*
* Copyright 2014 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onehippo.cms7.services.autoreload;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.onehippo.cms7.services.HippoServiceRegistry;
import org.onehippo.repository.modules.AbstractReconfigurableDaemonModule;
import org.onehippo.repository.modules.ProvidesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ProvidesService(types = { AutoReloadService.class })
public class AutoReloadServiceModule extends AbstractReconfigurableDaemonModule {
private static final Logger log = LoggerFactory.getLogger(AutoReloadServiceModule.class);
private final AutoReloadServiceConfig config;
private AutoReloadService service;
public AutoReloadServiceModule() {
config = new AutoReloadServiceConfig();
}
@Override
protected void doConfigure(final Node moduleConfig) throws RepositoryException {
config.reconfigure(moduleConfig);
log.info("Automatic reload of browsers is {}", config.isEnabled() ? "enabled" : "disabled");
}
@Override
protected void doInitialize(final Session session) throws RepositoryException {
service = new AutoReloadServiceImpl(config, new AutoReloadScriptLoader(), AutoReloadServer.getInstance());
HippoServiceRegistry.registerService(service, AutoReloadService.class);
}
@Override
protected void doShutdown() {
if (service != null) {
HippoServiceRegistry.unregisterService(service, AutoReloadService.class);
}
}
}
definitions:
config:
/hippo:configuration/hippo:modules/autoreload:
jcr:primaryType: hipposys:module
hipposys:className: org.onehippo.cms7.services.autoreload.AutoReloadServiceModule
/hippo:moduleconfig:
jcr:primaryType: hipposys:moduleconfig
enabled: true
group: hippo-cms
project:
name: hippo-services-autoreload
after: hippo-repository
module: hippo-services-autoreload
/*
* Copyright 2014 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function(window, console) {
var AUTO_RELOAD_PATH = "/autoreload",
CONTEXT_PATH = "/site",
RECONNECT_DELAY_MILLIS = 5000,
MAX_RECONNECT_ATTEMPTS = 120, // retry for 120 * 5000 ms = 10 minutes
hasBeenConnected = false,
isReloadingPage,
isReconnecting,
reconnectAttempts = 0,
websocket;
function serverUrl() {
return "ws://" + document.location.host + CONTEXT_PATH + AUTO_RELOAD_PATH;
}
function log(msg) {
if (typeof console.log === "function") {
console.log("Hippo auto-reload " + msg);
}
}
function reloadPage() {
log("is reloading page...");
isReloadingPage = true;
window.document.location.reload();
}
function onOpen() {
isReloadingPage = false;
isReconnecting = false;
hasBeenConnected = true;
log("connected");
}
function onMessage(event) {
var message = JSON.parse(event.data);
if (message.command === "reloadPage") {
reloadPage();
} else {
log("received unknown message:", message);
}
}
function onError(event) {
if (!isReconnecting) {
var warning = "error";
if (event.data) {
warning += ": " + event.data;
}
log(warning);
}
}
function onClose(event) {
if (!isReloadingPage && hasBeenConnected) {
reconnect();
}
}
function connect() {
log("connecting...");
websocket = new window.WebSocket(serverUrl());
websocket.onopen = onOpen;
websocket.onmessage = onMessage;
websocket.onerror = onError;
websocket.onclose = onClose;
}
function disconnect() {
if (websocket) {
isReloadingPage = true; // prevent reconnect
websocket.close();
}
}
function reconnect() {
if (reconnectAttempts < MAX_RECONNECT_ATTEMPTS) {
isReconnecting = true;
reconnectAttempts++;
log("disconnected, trying to reconnect...");
window.setTimeout(connect, RECONNECT_DELAY_MILLIS);
} else {
isReconnecting = false;
log("stopped trying to reconnect.");
}
}
if (window.addEventListener