001package net.gdface.utils;
002
003import java.io.ByteArrayInputStream;
004import java.io.File;
005import java.io.FileNotFoundException;
006import java.io.IOException;
007import java.util.Iterator;
008
009import javax.imageio.ImageIO;
010import javax.imageio.ImageReader;
011import javax.imageio.stream.MemoryCacheImageInputStream;
012
013public class FaceUtilitsX {
014
015        /**
016         * 通过 {@link ImageIO#getImageReaders(Object)}获取图像类型,返回文件格式名,如"jpeg","gif"<br>
017         * 用于保存图像文件时的后缀名
018         * @param img 图像数据
019         * @return
020         * @throws IllegalArgumentException {@code img}为null或空字符串""
021         * @throws IOException 
022         * @see ImageIO#getImageReaders(Object)
023         * @see ImageReader#getFormatName()
024         */
025        public static String getFormatName(byte[] img) throws IllegalArgumentException, IOException{
026                Assert.notEmpty(img, "img");
027                
028                Iterator<ImageReader> it = ImageIO.getImageReaders(new MemoryCacheImageInputStream(
029                                new ByteArrayInputStream(img)));
030                if (!it.hasNext())
031                        throw new IOException("UNRECOGNIZED IMG FORMAT");
032                ImageReader reader = it.next();
033                try{
034                        return reader.getFormatName();
035                }finally{
036                        reader.dispose();
037                }
038        
039        }
040
041        /**
042         * 当类被加载的时候,根据环境变量‘is_building’判断当前是运行状态还是代码编译构建状态<br>
043         * 如果没有定义环境变量,则默认为false<br>
044         * 
045         * @return
046         */
047        public static final boolean isBuilding() {
048                if (null == FaceUtilitsX.IS_BUILDING) {
049                        FaceUtilitsX.IS_BUILDING = Boolean.valueOf(FaceUtilitsX.getRuntimeProperty("is_building"));
050                }
051                return FaceUtilitsX.IS_BUILDING;
052        }
053
054        private static Boolean IS_BUILDING=null;
055
056        /**
057         * 通过环境变量CATALINA_HOME,获取axis2/WEB-INF/conf的位置<br>
058         * 如果没有定义CATALINA_HOME或没有找到/webapps/axis2/WEB-INF/conf,则抛出{@link FileNotFoundException}
059         * @return 返回conf文件夹位置
060         * @throws FileNotFoundException
061         */
062        public static File getAxis2Conf() throws FileNotFoundException{
063                String tomcat=System.getenv("CATALINA_HOME");
064                if(Judge.isEmpty(tomcat))
065                        throw new FileNotFoundException("NOT DEFINED environment variable CATALINA_HOME,can't locate configuration file");
066                File conf = new File(tomcat+"/webapps/axis2/WEB-INF/conf");
067                if(!conf.exists()||!conf.isDirectory())
068                        throw new FileNotFoundException(String.format("NOT FOUND %s for configuration file",conf));
069                return conf;
070        
071        }
072
073        /**
074         * 当类被加载的时候,根据环境变量‘notLoadCodeTable’判断是否要加载code表<br>
075         * 如果没有定义环境变量,则默认为false<br>
076         * @return
077         */
078        public static final boolean notLoadCodeTable() {
079                return Boolean.valueOf(FaceUtilitsX.getRuntimeProperty("notLoadCodeTable"));
080        }
081
082        public static final String getRuntimeProperty(String key){
083                String value = System.getProperty(key);
084                if (null == value)
085                        value = System.getenv(key);
086                System.out.printf("environment variable(java property)  %s=%b\n", key, Boolean.valueOf(value));
087                return value;
088        }
089
090}