Skip to content

Commit

Permalink
[GEOS-8039] Fix JMS plugin styles workspaces change handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuno Oliveira committed Mar 18, 2017
1 parent e9ab4c3 commit 059fd40
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 29 deletions.
Expand Up @@ -5,6 +5,7 @@
*/ */
package org.geoserver.cluster.impl.handlers.catalog; package org.geoserver.cluster.impl.handlers.catalog;


import java.io.ByteArrayInputStream;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.List; import java.util.List;


Expand All @@ -31,6 +32,8 @@
import org.geoserver.cluster.impl.utils.BeanUtils; import org.geoserver.cluster.impl.utils.BeanUtils;


import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.XStream;
import org.geoserver.cluster.server.events.StyleModifyEvent;
import org.geotools.renderer.style.Style;


/** /**
* *
Expand Down Expand Up @@ -288,6 +291,24 @@ protected static void modify(final Catalog catalog,


BeanUtils.smartUpdate(localObject, modifyEv.getPropertyNames(), BeanUtils.smartUpdate(localObject, modifyEv.getPropertyNames(),
modifyEv.getNewValues()); modifyEv.getNewValues());

// let's if the style file was provided
if (modifyEv instanceof StyleModifyEvent) {
StyleModifyEvent styleModifyEvent = (StyleModifyEvent) modifyEv;
byte[] fileContent = styleModifyEvent.getFile();
if (fileContent != null && fileContent.length != 0) {
// update the style file using the old style
StyleInfo oldStyle = catalog.getStyleByName(name);
try {
catalog.getResourcePool().writeStyle(oldStyle, new ByteArrayInputStream(fileContent));
} catch (Exception exception) {
throw new RuntimeException(String.format(
"Error writing style '%s' file.", localObject.getName()), exception);
}
}
}

// update the style in the catalog
catalog.save(localObject); catalog.save(localObject);


} else if (info instanceof WorkspaceInfo) { } else if (info instanceof WorkspaceInfo) {
Expand Down
Expand Up @@ -5,7 +5,9 @@
*/ */
package org.geoserver.cluster.server; package org.geoserver.cluster.server;


import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.OutputStream;
import java.util.Properties; import java.util.Properties;


import javax.jms.JMSException; import javax.jms.JMSException;
Expand All @@ -20,15 +22,20 @@
import org.geoserver.catalog.event.CatalogModifyEvent; import org.geoserver.catalog.event.CatalogModifyEvent;
import org.geoserver.catalog.event.CatalogPostModifyEvent; import org.geoserver.catalog.event.CatalogPostModifyEvent;
import org.geoserver.catalog.event.CatalogRemoveEvent; import org.geoserver.catalog.event.CatalogRemoveEvent;
import org.geoserver.catalog.impl.ModificationProxy;
import org.geoserver.cluster.JMSApplicationListener; import org.geoserver.cluster.JMSApplicationListener;
import org.geoserver.cluster.JMSPublisher; import org.geoserver.cluster.JMSPublisher;
import org.geoserver.cluster.impl.handlers.DocumentFile; import org.geoserver.cluster.impl.handlers.DocumentFile;
import org.geoserver.cluster.impl.utils.BeanUtils;
import org.geoserver.cluster.server.events.StyleModifyEvent;
import org.geoserver.config.GeoServerDataDirectory;
import org.geoserver.platform.GeoServerExtensions; import org.geoserver.platform.GeoServerExtensions;
import org.geoserver.platform.GeoServerResourceLoader; import org.geoserver.platform.GeoServerResourceLoader;
import org.geoserver.platform.resource.Paths;
import org.geoserver.platform.resource.Resource; import org.geoserver.platform.resource.Resource;
import org.geoserver.platform.resource.Resource.Type; import org.geoserver.platform.resource.Resource.Type;
import org.geoserver.platform.resource.Resources; import org.geoserver.platform.resource.Resources;
import org.geoserver.util.IOUtils;
import org.geotools.data.Base64;
import org.geotools.util.logging.Logging; import org.geotools.util.logging.Logging;


/** /**
Expand Down Expand Up @@ -166,33 +173,58 @@ public void handleModifyEvent(CatalogModifyEvent event) throws CatalogException
// update properties // update properties
Properties options = getProperties(); Properties options = getProperties();


try { // check if we may publish also the file
// check if we may publish also the file CatalogInfo info = event.getSource();
GeoServerResourceLoader loader = GeoServerExtensions.bean(GeoServerResourceLoader.class);
final CatalogInfo info = event.getSource();
if (info instanceof StyleInfo) {
// build local datadir file style path
Resource styleFile = loader.get("styles").get(((StyleInfo) info).getFilename());


if (!Resources.exists(styleFile)) { // if the modified object was a style we need to send the style file too
final String workspace = ((StyleInfo) info).getWorkspace().getName(); if (info instanceof StyleInfo) {
styleFile = loader.get(Paths.path("workspaces", workspace, "styles", // we need to get the associated resource file, for this we need to look
((StyleInfo) info).getFilename())); // at the final object we use a proxy to preserver the original object
StyleInfo styleInfo = ModificationProxy.create((StyleInfo) info, StyleInfo.class);
// updated the proxy object with the new values
try {
BeanUtils.smartUpdate(styleInfo, event.getPropertyNames(), event.getNewValues());
} catch (Exception exception) {
// there is nothing we can do about this
throw new RuntimeException(String.format(
"Error setting proxy of style '%s' new values.",
styleInfo.getName()), exception);
}
// get style associated resource
GeoServerDataDirectory dataDirectory = GeoServerExtensions.bean(GeoServerDataDirectory.class);
Resource resource = dataDirectory.get(styleInfo, styleInfo.getFilename());
if (!resource.file().exists()) {
// this should not happen we throw an exception
throw new RuntimeException(String.format(
"Style file '%s' for style '%s' could not be found.",
styleInfo.getFilename(), styleInfo.getName()));
}
try {
// read the style file to an array of bytes
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
IOUtils.copy(resource.in(), output);
} catch (Exception exception) {
throw new RuntimeException(String.format(
"Error reading style '%s' file '%s'.",
styleInfo.getName(), resource.file().getAbsolutePath()), exception);
} }

// publish the style event
// publish the style xml document jmsPublisher.publish(getTopic(), getJmsTemplate(), options, new StyleModifyEvent(event, output.toByteArray()));
jmsPublisher.publish(getTopic(), getJmsTemplate(), options, new DocumentFile(styleFile)); } catch (Exception exception) {
throw new RuntimeException(String.format(
"Error publishing file associated with style '%s'.",
styleInfo.getName()), exception);
} }

} else {
// propagate the event // propagate the catalog modified event
jmsPublisher.publish(getTopic(), getJmsTemplate(), options, event); try {

jmsPublisher.publish(getTopic(), getJmsTemplate(), options, event);
} catch (Exception e) { } catch (Exception exception) {
if (LOGGER.isLoggable(java.util.logging.Level.SEVERE)) { throw new RuntimeException(String.format(
LOGGER.severe(e.getLocalizedMessage()); "Error publishing catalog modified event of type '%s'.",
info.getClass().getSimpleName()), exception);
} }
final CatalogException ex = new CatalogException(e);
throw ex;
} }
} }


Expand Down
@@ -0,0 +1,48 @@
/* (c) 2017 Open Source Geospatial Foundation - all rights reserved
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.cluster.server.events;

import org.geoserver.catalog.CatalogInfo;
import org.geoserver.catalog.event.CatalogModifyEvent;

import java.util.List;

/**
* Catalog modify event for styles that include the style file as an array of bytes.
*/
public class StyleModifyEvent implements CatalogModifyEvent {

private final CatalogModifyEvent event;
private final byte[] file;

public StyleModifyEvent(CatalogModifyEvent event, byte[] file) {
this.event = event;
this.file = file;
}

@Override
public CatalogInfo getSource() {
return event.getSource();
}

@Override
public List<String> getPropertyNames() {
return event.getPropertyNames();
}

@Override
public List<Object> getOldValues() {
return event.getOldValues();
}

@Override
public List<Object> getNewValues() {
return event.getNewValues();
}

public byte[] getFile() {
return file;
}
}

0 comments on commit 059fd40

Please sign in to comment.