如何在 Jython/Websphere 中转义破折号?

发布于 2024-10-13 17:24:56 字数 586 浏览 3 评论 0原文

我有一个 Jython 脚本,用于在 Websphere 7.0 服务器上设置 JDBC 数据源。我需要在该数据源上设置几个属性。我正在使用这段代码,它有效,除非'-'

def setCustomProperty(datasource, name, value):
    parms = ['-propertyName', name, '-propertyValue', value]
    AdminTask.setResourceProperty(datasource, parms)

我需要将数据源上的 dateSeparator 属性设置为 - 破折号。当我使用 setCustomProperty(ds, 'dateSeparator', '-') 运行此脚本时,出现异常“无效属性:”。我发现它认为破折号意味着需要另一个参数/参数对。

有什么方法可以让 AdminTask 接受破折号吗?

注意:我无法通过 AdminConfig 设置它,因为我找不到获取正确属性 id 的方法(我有多个数据源)。

I have a Jython script that is used to set up a JDBC datasource on a Websphere 7.0 server. I need to set several properties on that datasource. I am using this code, which works, unless value is '-'.

def setCustomProperty(datasource, name, value):
    parms = ['-propertyName', name, '-propertyValue', value]
    AdminTask.setResourceProperty(datasource, parms)

I need to set the dateSeparator property on my datasource to just that - a dash. When I run this script with setCustomProperty(ds, 'dateSeparator', '-') I get an exception that says, "Invalid property: ". I figured out that it thinks that the dash means that another parameter/argument pair is expected.

Is there any way to get AdminTask to accept a dash?

NOTE: I can't set it via AdminConfig because I cannot find a way to get the id of the right property (I have multiple datasources).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

疯到世界奔溃 2024-10-20 17:24:56

下面是一个使用 AdminConfig 的解决方案,以便您可以将属性值设置为破折号 -。该解决方案考虑了多个数据源,通过指定适当的范围(即服务器,但如果您的数据源存在于不同的范围内,则可以对其进行修改)找到正确的数据源,然后按名称查找数据源。该解决方案还考虑修改现有的“dateSeparator”属性(如果存在),或者创建它(如果不存在)。

该代码看起来不太优雅,但我认为它应该可以解决您的问题:

def setDataSourceProperty(cell, node, server, ds, propName, propVal) :
  scopes = AdminConfig.getid("/Cell:%s/Node:%s/Server:%s/" % (cell, node, server)).splitlines()
  datasources = AdminConfig.list("DataSource", scopes[0]).splitlines()
  for datasource in datasources :
    if AdminConfig.showAttribute(datasource, "name") == ds :
      propertySet = AdminConfig.list("J2EEResourcePropertySet", datasource).splitlines()
      customProp = [["name", propName], ["value", propVal]]
      for property in AdminConfig.list("J2EEResourceProperty", propertySet[0]).splitlines() :
        if AdminConfig.showAttribute(property, "name") == propName :
          AdminConfig.modify(property, customProp)
          return
      AdminConfig.create("J2EEResourceProperty", propertySet[0], customProp)

if (__name__ == "__main__"):
  setDataSourceProperty("myCell01", "myNode01", "myServer", "myDataSource", "dateSeparator", "-")
  AdminConfig.save()

Here is a solution that uses AdminConfig so that you can set the property value to the dash -. The solution accounts for multiple data sources, finding the correct one by specifying the appropriate scope (i.e. the server, but this could be modified if your datasource exists within a different scope) and then finding the datasource by name. The solution also accounts for modifying the existing "dateSeparator" property if it exists, or it creates it if it doesn't.

The code doesn't look terribly elegant, but I think it should solve your problem :

def setDataSourceProperty(cell, node, server, ds, propName, propVal) :
  scopes = AdminConfig.getid("/Cell:%s/Node:%s/Server:%s/" % (cell, node, server)).splitlines()
  datasources = AdminConfig.list("DataSource", scopes[0]).splitlines()
  for datasource in datasources :
    if AdminConfig.showAttribute(datasource, "name") == ds :
      propertySet = AdminConfig.list("J2EEResourcePropertySet", datasource).splitlines()
      customProp = [["name", propName], ["value", propVal]]
      for property in AdminConfig.list("J2EEResourceProperty", propertySet[0]).splitlines() :
        if AdminConfig.showAttribute(property, "name") == propName :
          AdminConfig.modify(property, customProp)
          return
      AdminConfig.create("J2EEResourceProperty", propertySet[0], customProp)

if (__name__ == "__main__"):
  setDataSourceProperty("myCell01", "myNode01", "myServer", "myDataSource", "dateSeparator", "-")
  AdminConfig.save()
森林散布 2024-10-20 17:24:56

请参阅管理 控制台首选项设置。您可以执行您现在正在尝试的操作,并且您应该会看到管理控制台为自己使用而创建的 Jython 等效项。然后复制它即可。

Please see the Management Console preferences settings. You can do what you are attempting now and you should get to see the Jython equivalent that the Management Console is creating for its own use. Then just copy it.

林空鹿饮溪 2024-10-20 17:24:56

@Schemetrical 解决方案对我有用。只是给出另一个带有 jvm args 的例子。
不对实际答案发表评论,因为我没有足够的声誉。

server_name = 'server1'
AdminTask.setGenericJVMArguments('[ -serverName %s -genericJvmArguments "-agentlib:getClasses" ]' % (server_name))

@Schemetrical solution worked for me. Just giving another example with jvm args.
Not commenting on the actual answer because I don't have enough reputation.

server_name = 'server1'
AdminTask.setGenericJVMArguments('[ -serverName %s -genericJvmArguments "-agentlib:getClasses" ]' % (server_name))
残龙傲雪 2024-10-20 17:24:56

尝试使用字符串而不是数组来传递参数,并使用双引号将以破折号开头的值括起来

示例:

AdminTask.setVariable('-variableName JDK_PARAMS -variableValue "-Xlp -Xscm250M" -variableDescription "-Yes -I -can -now -use -dashes -everywhere :-)" -scope Cell=MyCell')

Try using a String instead of an array to pass the parameters using double quotes to surround the values starting with a dash sign

Example:

AdminTask.setVariable('-variableName JDK_PARAMS -variableValue "-Xlp -Xscm250M" -variableDescription "-Yes -I -can -now -use -dashes -everywhere :-)" -scope Cell=MyCell')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文