View Javadoc

1   /*
2    * Copyright 2004 Jeff Johnston
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.extremecomponents.util;
17  
18  import java.text.DecimalFormat;
19  import java.text.NumberFormat;
20  import java.text.ParseException;
21  import java.text.SimpleDateFormat;
22  import java.util.ArrayList;
23  import java.util.Date;
24  import java.util.Enumeration;
25  import java.util.List;
26  import java.util.Locale;
27  
28  import javax.servlet.http.HttpSession;
29  import javax.servlet.jsp.PageContext;
30  
31  import org.apache.commons.lang.StringUtils;
32  import org.apache.commons.lang.time.DateFormatUtils;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  
37  /***
38   * @author jeff johnston
39   */
40  public class ExtremeUtils
41  {
42  	private static Log logger = LogFactory.getLog(ExtremeUtils.class);
43  
44  	private ExtremeUtils()
45  	{
46  	}
47  
48  	/***
49  	 * Convert camelCase text to a readable word.
50  	 * example: camelCaseToWord --> Camel Case To Word
51  	 */
52  	public static String camelCaseToWord(String camelCaseText)
53  	{
54  		char[] ch = camelCaseText.toCharArray();
55  		String first = "" + ch[0];
56  		String build = first.toUpperCase();
57  
58  		for (int i = 1; i < ch.length; i++)
59  		{
60  			String test = "" + ch[i];
61  
62  			if (test.equals(test.toUpperCase()))
63  			{
64  				build += " ";
65  			}
66  
67  			build += test;
68  		}
69  
70  		return build;
71  	}
72  
73  	public static String formatDate(String parse, String format, Object value)
74  	{
75  		String result = null;
76  
77  		try
78  		{
79  			if (value != null)
80  			{
81  				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(parse);
82  				Date date = simpleDateFormat.parse(value.toString());
83  				result = DateFormatUtils.format(date, format);
84  			}
85  		}
86  		catch (ParseException e)
87  		{
88  			e.printStackTrace();
89  		}
90  
91  		return result;
92  	}
93  
94  	public static String formatNumber(String format, String value)
95  	{
96  		String result = null;
97  
98  		if ((value != null) && (format != null))
99  		{
100 			NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
101 			DecimalFormat df = (DecimalFormat) nf;
102 			df.applyPattern(format);
103 
104 			double d = Double.parseDouble(value);
105 			result = df.format(d);
106 		}
107 
108 		return result;
109 	}
110 
111 	/***
112 	 * Get the bean out of the proper scope.
113 	 */
114 	public static Object retrieveFromScope(PageContext pageContext, String name, String scope)
115 	{
116 		if (StringUtils.isBlank(scope))
117 		{
118 			return pageContext.findAttribute(name);
119 		}
120 		else
121 		{
122 			int scopeType = PageContext.REQUEST_SCOPE;
123 
124 			if (scope.equalsIgnoreCase("page"))
125 			{
126 				scopeType = PageContext.PAGE_SCOPE;
127 			}
128 			else if (scope.equalsIgnoreCase("application"))
129 			{
130 				scopeType = PageContext.APPLICATION_SCOPE;
131 			}
132 			else if (scope.equalsIgnoreCase("session"))
133 			{
134 				scopeType = PageContext.SESSION_SCOPE;
135 			}
136 
137 			return pageContext.getAttribute(name, scopeType);
138 		}
139 	}
140 
141 	public static int sessionSize(HttpSession session)
142 	{
143 		int total = 0;
144 
145 		try
146 		{
147 			java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
148 			java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(baos);
149 			Enumeration enumeration = session.getAttributeNames();
150 			List test = new ArrayList();
151 
152 			while (enumeration.hasMoreElements())
153 			{
154 				String name = (String) enumeration.nextElement();
155 				Object obj = session.getAttribute(name);
156 				oos.writeObject(obj);
157 
158 				int size = baos.size();
159 				total += size;
160 				logger.debug("The session name: " + name + " and the size is: " + size);
161 			}
162 
163 			logger.debug("Total session size is: " + total);
164 		}
165 		catch (Exception e)
166 		{
167 			logger.error("Could not get the session size", e);
168 			e.printStackTrace();
169 		}
170 
171 		return total;
172 	}
173 }