dev@glassfish.java.net

Re: [Ask]About Java System Property Setting

From: Tang Yong <tangyong_at_cn.fujitsu.com>
Date: Sun, 17 Feb 2013 17:01:04 +0900

Hi Ancoron,

Thanks your reply very much and python script.

Also thanks your offering "asadmin osgi ..." command, it is very simple
and useful.

-Tang

Ancoron Luciferis wrote:
> Hi Tang,
>
> if you need system properties to be available immediately at domain
> startup time you may need to include them manually into the
> configuration file:
>
> .../<domain-dir>/config/domain.xml
>
> Your options would then go into the appropriate section:
>
> /domain/configs/config/java-config/
>
> ...and would look like this:
>
> <jvm-options>http.proxyHost=XXXXX</jvm-options>
> <jvm-options>http.proxyPort=XXXXX</jvm-options>
>
> This is essentially the same as what "asadmin create-jvm-options" does.
>
> I've also written a little python script for this task (and some
> others), which I attached for convenience (just use "-h" for a
> comprehensive help).
>
> Hope I could help out.
>
> Cheers,
>
> Ancoron
>
>
> On 01/29/2013 03:46 AM, Tang Yong wrote:
>> Hi Sahoo
>> CC Team
>>
>> I have a question about Java System Property Setting:
>>
>> [Background]
>> I access internet behind a http proxy. I know that after starting
>> glassfish domain, I can set jvm options(http proxy) by the following,
>>
>> asadmin create-jvm-options -Dhttp.proxyHost=XXXXX
>> asadmin create-jvm-options -Dhttp.proxyPort=XXXXX
>>
>> Then, restarting domain will OK. However, imaging such a scene:
>>
>> If I want to access internet's resource while starting glassfish domain
>> rather than after starting glassfish domain. For example, I have a
>> bundle which is put into autostart directory and I want to access
>> http://repo1.maven.org/maven2/... to generate OSGi OBR resources to meet
>> some provisioning stories by the bundle. Then, current glassfish does
>> not meet such a requirement because gf can not offer a way to set
>> http.proxyHost and http.proxyPort while starting gf domain.
>>
>> I have tried to add these system properties into config/osgi.properties,
>> however, this have not any effect.
>>
>> Noting that in [1] , the following is said:
>>
>> "These system properties can be set directly on the command line when
>> starting the JVM using the standard "-D<prop>=<value>" syntax or you can
>> put them in the lib/system.properties file of your Felix installation;
>> see the next section on configuring Felix for more information."
>>
>> So, while launching felix, whether gf can offer some way to load system
>> properties or not?
>>
>> [1]:
>> http://felix.apache.org/site/apache-felix-framework-usage-documentation.html
>>
>> Thanks
>> --Tang
>>
>>
>
>
> ------------------------------------------------------------------------
>
> #!/usr/bin/env python
> #
> # Copyright 2012 ancoron.
> #
> # 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.
>
> import sys
> import os
> import argparse
> import re
>
> def main():
> try:
> from lxml import etree
> except ImportError:
> try:
> # Python 2.5
> import xml.etree.cElementTree as etree
> except ImportError:
> try:
> # Python 2.5
> import xml.etree.ElementTree as etree
> except ImportError:
> try:
> # normal cElementTree install
> import cElementTree as etree
> except ImportError:
> try:
> # normal ElementTree install
> import elementtree.ElementTree as etree
> except ImportError:
> print("Failed to import ElementTree from any known place")
> sys.exit(1)
>
>
> parser = argparse.ArgumentParser(
> formatter_class=argparse.RawTextHelpFormatter,
> description='List or modify persistent JVM options for a GlassFish Domain.',
> epilog="""The command arguments (arg) are as follows:
>
> Command Required arguments
>
> list None
>
> set <property-name> <property-value>
>
> delete <property-name>
>
> """
> )
> parser.add_argument('domainXml', type=file,
> help='The path to the GlassFish Domain configuration file (domain.xml)')
> parser.add_argument('command', choices=['list','set','delete'],
> help='What to do with the JVM options')
> parser.add_argument('-c', '--config', default='server-config',
> metavar='name',
> help='Name of the configuration within the specified domain',
> required=False
> )
> parser.add_argument('args', metavar='...', nargs=argparse.REMAINDER,
> help='Optional arguments for the specific command')
>
> args = parser.parse_args()
>
> docPath = args.domainXml
> conf = args.config
>
> if args.command == 'list':
> doc = etree.parse(docPath)
> options = doc.xpath('//domain/configs/config[@name="{0}"]/java-config'.format(conf))[0]
> for element in options.iter():
> if etree.iselement(element) and element.tag == "jvm-options":
> print(element.text)
>
> elif args.command == 'set':
> if len(args.args) == 0:
> print("Please specify property name and value")
> sys.exit(1)
> elif len(args.args) == 1 and "=" in args.args[0]:
> # print("Please specify property name AND value in separate arguments. Working around here...")
> a = args.args[0].split("=")
> args.args[0] = a[0]
> args.args.append(a[1])
> elif len(args.args) > 2:
> print("Ignoring additional command arguments.")
>
> newProperty = args.args[0]
> newValue = None
> if len(args.args) > 1:
> newValue = args.args[1]
>
> doc = etree.parse(docPath)
> options = doc.xpath('//domain/configs/config[@name="{0}"]/java-config'.format(conf))[0]
> found = False
> txt = None
> if newValue == None:
> txt = newProperty
> else:
> txt = "{0}={1}".format(newProperty, newValue)
>
> for element in options.iter():
> eProp = element.text.split("=")
> eName = eProp[0]
> if eName == newProperty:
> element.text = txt
> found = True
>
> if not found:
> etree.SubElement(options, 'jvm-options').text = txt
>
> domainXml = open(docPath.name, 'w')
> doc.write(domainXml)
> sys.exit(0)
>
> elif args.command == 'delete':
> if len(args.args) == 0:
> print("Please specify property name and value")
> sys.exit(1)
> elif len(args.args) > 1:
> print("Ignoring additional command arguments.")
>
> newProperty = args.args[0]
>
> doc = etree.parse(docPath)
> options = doc.xpath('//domain/configs/config[@name="{0}"]/java-config'.format(conf))[0]
> found = []
> search = "^{0}(|=.*)$".format(newProperty)
> for element in options.iter():
> eProp = element.text.split("=")
> eName = eProp[0]
> if eName == newProperty:
> found.append(element)
>
> if len(found) == 0:
> print("JVM-Option '{0}' not found.".format(newProperty))
> sys.exit(0)
> else:
> for e in found:
> options.remove(e)
>
> domainXml = open(docPath.name, 'w')
> doc.write(domainXml)
> sys.exit(0)
>
> else:
> print("Sub-command '%s' not available!" % args[1])
> sys.exit(1)
>
> if __name__ == '__main__':
> main()