GEODE-3436: Restore refactoring of PDXCommands * See initial commit GEODE-3266 (67185abcdd68b908dea6888cb94286b8aa9ea49f)
Project: http://git-wip-us.apache.org/repos/asf/geode/repo Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/50dd10b7 Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/50dd10b7 Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/50dd10b7 Branch: refs/heads/develop Commit: 50dd10b76bf1264aa3e89bdfff2d9feda3facf44 Parents: 7ff9539 Author: YehEmily <emilyyeh1...@gmail.com> Authored: Fri Aug 4 10:47:48 2017 -0700 Committer: Jinmei Liao <jil...@pivotal.io> Committed: Tue Aug 29 09:27:23 2017 -0700 ---------------------------------------------------------------------- .../cli/commands/ConfigurePDXCommand.java | 138 +++++++++++++ .../internal/cli/commands/PDXCommands.java | 195 ------------------- .../internal/cli/commands/PDXRenameCommand.java | 81 ++++++++ .../web/controllers/PdxCommandsController.java | 3 +- 4 files changed, 221 insertions(+), 196 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/geode/blob/50dd10b7/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/ConfigurePDXCommand.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/ConfigurePDXCommand.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/ConfigurePDXCommand.java new file mode 100644 index 0000000..8c490f4 --- /dev/null +++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/ConfigurePDXCommand.java @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You 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.apache.geode.management.internal.cli.commands; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.Arrays; + +import org.springframework.shell.core.annotation.CliCommand; +import org.springframework.shell.core.annotation.CliOption; + +import org.apache.geode.internal.cache.CacheConfig; +import org.apache.geode.internal.cache.xmlcache.CacheCreation; +import org.apache.geode.internal.cache.xmlcache.CacheXml; +import org.apache.geode.internal.cache.xmlcache.CacheXmlGenerator; +import org.apache.geode.management.cli.CliMetaData; +import org.apache.geode.management.cli.Result; +import org.apache.geode.management.internal.cli.CliUtil; +import org.apache.geode.management.internal.cli.i18n.CliStrings; +import org.apache.geode.management.internal.cli.result.InfoResultData; +import org.apache.geode.management.internal.cli.result.ResultBuilder; +import org.apache.geode.management.internal.configuration.domain.XmlEntity; +import org.apache.geode.management.internal.security.ResourceOperation; +import org.apache.geode.pdx.ReflectionBasedAutoSerializer; +import org.apache.geode.security.ResourcePermission; + +public class ConfigurePDXCommand implements GfshCommand { + @CliCommand(value = CliStrings.CONFIGURE_PDX, help = CliStrings.CONFIGURE_PDX__HELP) + @CliMetaData(relatedTopic = CliStrings.TOPIC_GEODE_REGION) + @ResourceOperation(resource = ResourcePermission.Resource.CLUSTER, + operation = ResourcePermission.Operation.MANAGE) + public Result configurePDX( + @CliOption(key = CliStrings.CONFIGURE_PDX__READ__SERIALIZED, + help = CliStrings.CONFIGURE_PDX__READ__SERIALIZED__HELP) Boolean readSerialized, + @CliOption(key = CliStrings.CONFIGURE_PDX__IGNORE__UNREAD_FIELDS, + help = CliStrings.CONFIGURE_PDX__IGNORE__UNREAD_FIELDS__HELP) Boolean ignoreUnreadFields, + @CliOption(key = CliStrings.CONFIGURE_PDX__DISKSTORE, specifiedDefaultValue = "", + help = CliStrings.CONFIGURE_PDX__DISKSTORE__HELP) String diskStore, + @CliOption(key = CliStrings.CONFIGURE_PDX__AUTO__SERIALIZER__CLASSES, + help = CliStrings.CONFIGURE_PDX__AUTO__SERIALIZER__CLASSES__HELP) String[] patterns, + @CliOption(key = CliStrings.CONFIGURE_PDX__PORTABLE__AUTO__SERIALIZER__CLASSES, + help = CliStrings.CONFIGURE_PDX__PORTABLE__AUTO__SERIALIZER__CLASSES__HELP) String[] portablePatterns) { + Result result; + + try { + InfoResultData ird = ResultBuilder.createInfoResultData(); + CacheCreation cache = new CacheCreation(true); + + if ((portablePatterns != null && portablePatterns.length > 0) + && (patterns != null && patterns.length > 0)) { + return ResultBuilder.createUserErrorResult(CliStrings.CONFIGURE_PDX__ERROR__MESSAGE); + } + if (!CliUtil.getAllNormalMembers(CliUtil.getCacheIfExists()).isEmpty()) { + ird.addLine(CliStrings.CONFIGURE_PDX__NORMAL__MEMBERS__WARNING); + } + // Set persistent and the disk-store + if (diskStore != null) { + cache.setPdxPersistent(true); + ird.addLine(CliStrings.CONFIGURE_PDX__PERSISTENT + " = " + cache.getPdxPersistent()); + if (!diskStore.equals("")) { + cache.setPdxDiskStore(diskStore); + ird.addLine(CliStrings.CONFIGURE_PDX__DISKSTORE + " = " + cache.getPdxDiskStore()); + } else { + ird.addLine(CliStrings.CONFIGURE_PDX__DISKSTORE + " = " + "DEFAULT"); + } + } else { + cache.setPdxPersistent(CacheConfig.DEFAULT_PDX_PERSISTENT); + ird.addLine(CliStrings.CONFIGURE_PDX__PERSISTENT + " = " + cache.getPdxPersistent()); + } + + // Set read-serialized + if (readSerialized != null) { + cache.setPdxReadSerialized(readSerialized); + } else { + cache.setPdxReadSerialized(CacheConfig.DEFAULT_PDX_READ_SERIALIZED); + } + ird.addLine( + CliStrings.CONFIGURE_PDX__READ__SERIALIZED + " = " + cache.getPdxReadSerialized()); + + + // Set ignoreUnreadFields + if (ignoreUnreadFields != null) { + cache.setPdxIgnoreUnreadFields(ignoreUnreadFields); + } else { + cache.setPdxIgnoreUnreadFields(CacheConfig.DEFAULT_PDX_IGNORE_UNREAD_FIELDS); + } + ird.addLine(CliStrings.CONFIGURE_PDX__IGNORE__UNREAD_FIELDS + " = " + + cache.getPdxIgnoreUnreadFields()); + + + if (portablePatterns != null) { + ReflectionBasedAutoSerializer autoSerializer = + new ReflectionBasedAutoSerializer(portablePatterns); + cache.setPdxSerializer(autoSerializer); + ird.addLine("PDX Serializer " + cache.getPdxSerializer().getClass().getName()); + ird.addLine("Portable classes " + Arrays.toString(portablePatterns)); + } + + if (patterns != null) { + ReflectionBasedAutoSerializer nonPortableAutoSerializer = + new ReflectionBasedAutoSerializer(true, patterns); + cache.setPdxSerializer(nonPortableAutoSerializer); + ird.addLine("PDX Serializer : " + cache.getPdxSerializer().getClass().getName()); + ird.addLine("Non portable classes :" + Arrays.toString(patterns)); + } + + final StringWriter stringWriter = new StringWriter(); + final PrintWriter printWriter = new PrintWriter(stringWriter); + CacheXmlGenerator.generate(cache, printWriter, true, false, false); + printWriter.close(); + String xmlDefinition = stringWriter.toString(); + // TODO jbarrett - shouldn't this use the same loadXmlDefinition that other constructors use? + XmlEntity xmlEntity = + XmlEntity.builder().withType(CacheXml.PDX).withConfig(xmlDefinition).build(); + + result = ResultBuilder.buildResult(ird); + persistClusterConfiguration(result, + () -> getSharedConfiguration().addXmlEntity(xmlEntity, null)); + + } catch (Exception e) { + return ResultBuilder.createGemFireErrorResult(e.getMessage()); + } + return result; + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/50dd10b7/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/PDXCommands.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/PDXCommands.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/PDXCommands.java deleted file mode 100644 index e0bad15..0000000 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/PDXCommands.java +++ /dev/null @@ -1,195 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license - * agreements. See the NOTICE file distributed with this work for additional information regarding - * copyright ownership. The ASF licenses this file to You 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.apache.geode.management.internal.cli.commands; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.PrintStream; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.util.Arrays; -import java.util.Collection; - -import org.springframework.shell.core.annotation.CliCommand; -import org.springframework.shell.core.annotation.CliOption; - -import org.apache.geode.internal.cache.CacheConfig; -import org.apache.geode.internal.cache.DiskStoreImpl; -import org.apache.geode.internal.cache.xmlcache.CacheCreation; -import org.apache.geode.internal.cache.xmlcache.CacheXml; -import org.apache.geode.internal.cache.xmlcache.CacheXmlGenerator; -import org.apache.geode.management.cli.CliMetaData; -import org.apache.geode.management.cli.Result; -import org.apache.geode.management.internal.cli.CliUtil; -import org.apache.geode.management.internal.cli.i18n.CliStrings; -import org.apache.geode.management.internal.cli.result.InfoResultData; -import org.apache.geode.management.internal.cli.result.ResultBuilder; -import org.apache.geode.management.internal.configuration.domain.XmlEntity; -import org.apache.geode.management.internal.security.ResourceOperation; -import org.apache.geode.pdx.ReflectionBasedAutoSerializer; -import org.apache.geode.pdx.internal.EnumInfo; -import org.apache.geode.pdx.internal.PdxType; -import org.apache.geode.security.ResourcePermission.Operation; -import org.apache.geode.security.ResourcePermission.Resource; - -public class PDXCommands implements GfshCommand { - - - @CliCommand(value = CliStrings.CONFIGURE_PDX, help = CliStrings.CONFIGURE_PDX__HELP) - @CliMetaData(relatedTopic = CliStrings.TOPIC_GEODE_REGION) - @ResourceOperation(resource = Resource.CLUSTER, operation = Operation.MANAGE) - public Result configurePDX( - @CliOption(key = CliStrings.CONFIGURE_PDX__READ__SERIALIZED, - help = CliStrings.CONFIGURE_PDX__READ__SERIALIZED__HELP) Boolean readSerialized, - @CliOption(key = CliStrings.CONFIGURE_PDX__IGNORE__UNREAD_FIELDS, - help = CliStrings.CONFIGURE_PDX__IGNORE__UNREAD_FIELDS__HELP) Boolean ignoreUnreadFields, - @CliOption(key = CliStrings.CONFIGURE_PDX__DISKSTORE, specifiedDefaultValue = "", - help = CliStrings.CONFIGURE_PDX__DISKSTORE__HELP) String diskStore, - @CliOption(key = CliStrings.CONFIGURE_PDX__AUTO__SERIALIZER__CLASSES, - help = CliStrings.CONFIGURE_PDX__AUTO__SERIALIZER__CLASSES__HELP) String[] patterns, - @CliOption(key = CliStrings.CONFIGURE_PDX__PORTABLE__AUTO__SERIALIZER__CLASSES, - help = CliStrings.CONFIGURE_PDX__PORTABLE__AUTO__SERIALIZER__CLASSES__HELP) String[] portablePatterns) { - Result result; - - try { - InfoResultData ird = ResultBuilder.createInfoResultData(); - CacheCreation cache = new CacheCreation(true); - - if ((portablePatterns != null && portablePatterns.length > 0) - && (patterns != null && patterns.length > 0)) { - return ResultBuilder.createUserErrorResult(CliStrings.CONFIGURE_PDX__ERROR__MESSAGE); - } - if (!CliUtil.getAllNormalMembers(CliUtil.getCacheIfExists()).isEmpty()) { - ird.addLine(CliStrings.CONFIGURE_PDX__NORMAL__MEMBERS__WARNING); - } - // Set persistent and the disk-store - if (diskStore != null) { - cache.setPdxPersistent(true); - ird.addLine(CliStrings.CONFIGURE_PDX__PERSISTENT + " = " + cache.getPdxPersistent()); - if (!diskStore.equals("")) { - cache.setPdxDiskStore(diskStore); - ird.addLine(CliStrings.CONFIGURE_PDX__DISKSTORE + " = " + cache.getPdxDiskStore()); - } else { - ird.addLine(CliStrings.CONFIGURE_PDX__DISKSTORE + " = " + "DEFAULT"); - } - } else { - cache.setPdxPersistent(CacheConfig.DEFAULT_PDX_PERSISTENT); - ird.addLine(CliStrings.CONFIGURE_PDX__PERSISTENT + " = " + cache.getPdxPersistent()); - } - - // Set read-serialized - if (readSerialized != null) { - cache.setPdxReadSerialized(readSerialized); - } else { - cache.setPdxReadSerialized(CacheConfig.DEFAULT_PDX_READ_SERIALIZED); - } - ird.addLine( - CliStrings.CONFIGURE_PDX__READ__SERIALIZED + " = " + cache.getPdxReadSerialized()); - - - // Set ignoreUnreadFields - if (ignoreUnreadFields != null) { - cache.setPdxIgnoreUnreadFields(ignoreUnreadFields); - } else { - cache.setPdxIgnoreUnreadFields(CacheConfig.DEFAULT_PDX_IGNORE_UNREAD_FIELDS); - } - ird.addLine(CliStrings.CONFIGURE_PDX__IGNORE__UNREAD_FIELDS + " = " - + cache.getPdxIgnoreUnreadFields()); - - - if (portablePatterns != null) { - ReflectionBasedAutoSerializer autoSerializer = - new ReflectionBasedAutoSerializer(portablePatterns); - cache.setPdxSerializer(autoSerializer); - ird.addLine("PDX Serializer " + cache.getPdxSerializer().getClass().getName()); - ird.addLine("Portable classes " + Arrays.toString(portablePatterns)); - } - - if (patterns != null) { - ReflectionBasedAutoSerializer nonPortableAutoSerializer = - new ReflectionBasedAutoSerializer(true, patterns); - cache.setPdxSerializer(nonPortableAutoSerializer); - ird.addLine("PDX Serializer : " + cache.getPdxSerializer().getClass().getName()); - ird.addLine("Non portable classes :" + Arrays.toString(patterns)); - } - - final StringWriter stringWriter = new StringWriter(); - final PrintWriter printWriter = new PrintWriter(stringWriter); - CacheXmlGenerator.generate(cache, printWriter, true, false, false); - printWriter.close(); - String xmlDefinition = stringWriter.toString(); - // TODO jbarrett - shouldn't this use the same loadXmlDefinition that other constructors use? - XmlEntity xmlEntity = - XmlEntity.builder().withType(CacheXml.PDX).withConfig(xmlDefinition).build(); - - result = ResultBuilder.buildResult(ird); - persistClusterConfiguration(result, - () -> getSharedConfiguration().addXmlEntity(xmlEntity, null)); - - } catch (Exception e) { - return ResultBuilder.createGemFireErrorResult(e.getMessage()); - } - - return result; - } - - @CliCommand(value = CliStrings.PDX_RENAME, help = CliStrings.PDX_RENAME__HELP) - @CliMetaData(shellOnly = true, relatedTopic = {CliStrings.TOPIC_GEODE_DISKSTORE}) - public Result pdxRename(@CliOption(key = CliStrings.PDX_RENAME_OLD, mandatory = true, - help = CliStrings.PDX_RENAME_OLD__HELP) String oldClassName, - - @CliOption(key = CliStrings.PDX_RENAME_NEW, mandatory = true, - help = CliStrings.PDX_RENAME_NEW__HELP) String newClassName, - - @CliOption(key = CliStrings.PDX_DISKSTORE, mandatory = true, - help = CliStrings.PDX_DISKSTORE__HELP) String diskStore, - - @CliOption(key = CliStrings.PDX_DISKDIR, mandatory = true, - help = CliStrings.PDX_DISKDIR__HELP) String[] diskDirs) { - - try { - final File[] dirs = new File[diskDirs.length]; - for (int i = 0; i < diskDirs.length; i++) { - dirs[i] = new File((diskDirs[i])); - } - - Collection<Object> results = - DiskStoreImpl.pdxRename(diskStore, dirs, oldClassName, newClassName); - - if (results.isEmpty()) { - return ResultBuilder - .createGemFireErrorResult(CliStrings.format(CliStrings.PDX_RENAME__EMPTY)); - } - - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - PrintStream printStream = new PrintStream(outputStream); - for (Object p : results) { - if (p instanceof PdxType) { - ((PdxType) p).toStream(printStream, false); - } else { - ((EnumInfo) p).toStream(printStream); - } - } - String resultString = - CliStrings.format(CliStrings.PDX_RENAME__SUCCESS, outputStream.toString()); - return ResultBuilder.createInfoResult(resultString); - - } catch (Exception e) { - return ResultBuilder.createGemFireErrorResult( - CliStrings.format(CliStrings.PDX_RENAME__ERROR, e.getMessage())); - } - - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/50dd10b7/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/PDXRenameCommand.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/PDXRenameCommand.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/PDXRenameCommand.java new file mode 100644 index 0000000..8831597 --- /dev/null +++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/PDXRenameCommand.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You 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.apache.geode.management.internal.cli.commands; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.PrintStream; +import java.util.Collection; + +import org.springframework.shell.core.annotation.CliCommand; +import org.springframework.shell.core.annotation.CliOption; + +import org.apache.geode.internal.cache.DiskStoreImpl; +import org.apache.geode.management.cli.CliMetaData; +import org.apache.geode.management.cli.Result; +import org.apache.geode.management.internal.cli.i18n.CliStrings; +import org.apache.geode.management.internal.cli.result.ResultBuilder; +import org.apache.geode.pdx.internal.EnumInfo; +import org.apache.geode.pdx.internal.PdxType; + +public class PDXRenameCommand implements GfshCommand { + @CliCommand(value = CliStrings.PDX_RENAME, help = CliStrings.PDX_RENAME__HELP) + @CliMetaData(shellOnly = true, relatedTopic = {CliStrings.TOPIC_GEODE_DISKSTORE}) + public Result pdxRename(@CliOption(key = CliStrings.PDX_RENAME_OLD, mandatory = true, + help = CliStrings.PDX_RENAME_OLD__HELP) String oldClassName, + + @CliOption(key = CliStrings.PDX_RENAME_NEW, mandatory = true, + help = CliStrings.PDX_RENAME_NEW__HELP) String newClassName, + + @CliOption(key = CliStrings.PDX_DISKSTORE, mandatory = true, + help = CliStrings.PDX_DISKSTORE__HELP) String diskStore, + + @CliOption(key = CliStrings.PDX_DISKDIR, mandatory = true, + help = CliStrings.PDX_DISKDIR__HELP) String[] diskDirs) { + + try { + final File[] dirs = new File[diskDirs.length]; + for (int i = 0; i < diskDirs.length; i++) { + dirs[i] = new File((diskDirs[i])); + } + + Collection<Object> results = + DiskStoreImpl.pdxRename(diskStore, dirs, oldClassName, newClassName); + + if (results.isEmpty()) { + return ResultBuilder + .createGemFireErrorResult(CliStrings.format(CliStrings.PDX_RENAME__EMPTY)); + } + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + PrintStream printStream = new PrintStream(outputStream); + for (Object p : results) { + if (p instanceof PdxType) { + ((PdxType) p).toStream(printStream, false); + } else { + ((EnumInfo) p).toStream(printStream); + } + } + String resultString = + CliStrings.format(CliStrings.PDX_RENAME__SUCCESS, outputStream.toString()); + return ResultBuilder.createInfoResult(resultString); + + } catch (Exception e) { + return ResultBuilder.createGemFireErrorResult( + CliStrings.format(CliStrings.PDX_RENAME__ERROR, e.getMessage())); + } + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/50dd10b7/geode-core/src/main/java/org/apache/geode/management/internal/web/controllers/PdxCommandsController.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/web/controllers/PdxCommandsController.java b/geode-core/src/main/java/org/apache/geode/management/internal/web/controllers/PdxCommandsController.java index c757fd3..2722964 100644 --- a/geode-core/src/main/java/org/apache/geode/management/internal/web/controllers/PdxCommandsController.java +++ b/geode-core/src/main/java/org/apache/geode/management/internal/web/controllers/PdxCommandsController.java @@ -27,7 +27,8 @@ import org.springframework.web.bind.annotation.ResponseBody; * The PdxCommandsController class implements GemFire Management REST API web service endpoints for * Gfsh PDX Commands. * - * @see org.apache.geode.management.internal.cli.commands.PDXCommands + * @see org.apache.geode.management.internal.cli.commands.PDXRenameCommand + * @see org.apache.geode.management.internal.cli.commands.ConfigurePDXCommand * @see org.apache.geode.management.internal.web.controllers.AbstractCommandsController * @see org.springframework.stereotype.Controller * @see org.springframework.web.bind.annotation.RequestMapping