Skip to content

Instantly share code, notes, and snippets.

@abstractj
Created February 6, 2012 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abstractj/1753888 to your computer and use it in GitHub Desktop.
Save abstractj/1753888 to your computer and use it in GitHub Desktop.
TimeIntervalUtil
/*
* Copyright 2008-2012 Red Hat, Inc, and individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.projectodd.polyglot.core.util;
import java.util.concurrent.TimeUnit;
public class TimeIntervalUtil {
public static IntervalData parseInterval(String data, TimeUnit defaultUnit) {
TimeUnit timeUnit = defaultUnit;
//if (data != null) {
data = data != null ? data.trim() : "";
if (data.endsWith( "ms" )) {
timeUnit = TimeUnit.MILLISECONDS;
data = data.substring( 0, data.length() - 2 );
} else if (data.endsWith( "m" )) {
timeUnit = TimeUnit.MINUTES;
data = data.substring( 0, data.length() - 1 );
} else if (data.endsWith( "h" )) {
timeUnit = TimeUnit.HOURS;
data = data.substring( 0, data.length() - 1 );
} else if (data.endsWith( "s" )) {
timeUnit = TimeUnit.SECONDS;
data = data.substring( 0, data.length() - 1 );
}
return new IntervalData( Long.parseLong( data.trim() ), timeUnit );
//}
//return new IntervalData();
}
public static class IntervalData {
public long interval;
public TimeUnit unit = TimeUnit.MINUTES;
public IntervalData(){};
public IntervalData(long interval, TimeUnit unit) {
this.interval = interval;
this.unit = unit;
}
}
}
/*
* Copyright 2008-2012 Red Hat, Inc, and individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.torquebox.web.rack.processors;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.jboss.as.server.deployment.DeploymentUnit;
import org.jboss.logging.Logger;
import org.projectodd.polyglot.core.util.TimeIntervalUtil;
import org.torquebox.core.processors.AbstractSplitYamlParsingProcessor;
import org.torquebox.web.rack.RackMetaData;
public class WebYamlParsingProcessor extends AbstractSplitYamlParsingProcessor {
public WebYamlParsingProcessor() {
setSectionName( "web" );
setSupportsStandalone( false );
}
@SuppressWarnings("unchecked")
public void parse(DeploymentUnit unit, Object dataObj) throws Exception {
RackMetaData rackAppMetaData = unit.getAttachment( RackMetaData.ATTACHMENT_KEY );
if (rackAppMetaData == null) {
rackAppMetaData = new RackMetaData();
rackAppMetaData.attachTo( unit );
}
Map<String, Object> webData = (Map<String, Object>) dataObj;
rackAppMetaData.setContextPath( (String) webData.get( "context" ) );
rackAppMetaData.setStaticPathPrefix( (String) webData.get( "static" ) );
if (webData.get( "rackup" ) != null) {
rackAppMetaData.setRackUpScriptLocation( (String) webData.get( "rackup" ) );
}
Object hosts = webData.get( "host" );
if (hosts instanceof List) {
List<String> list = (List<String>) hosts;
for (String each : list) {
rackAppMetaData.addHost( each );
}
} else {
rackAppMetaData.addHost( (String) hosts );
}
String timeoutStr = webData.containsKey( "session-timeout" ) ?
webData.get( "session-timeout" ).toString() : null;
TimeIntervalUtil.IntervalData intervalData = TimeIntervalUtil.parseInterval(timeoutStr, TimeUnit.MINUTES);
rackAppMetaData.setSessionTimeout(intervalData.interval, intervalData.unit);
}
@SuppressWarnings("unused")
private static final Logger log = Logger.getLogger( "org.torquebox.web.rack" );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment