liuxiaolong
2019-05-09 0d1d88cdb668e75ea8609417ac18ae19947e9525
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package framework.startup;
 
import java.io.File;
 
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
 
import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
 
/**
 * 框架需提供的工具类
 * 
 * @author liuyajun, 8384503@qq.com
 * @date 2016年1月11日
 * @time 下午8:37:38
 */
public final class FrameUtil {
    private static String webRoot = null;
    private static String webInf = null;
    private static String uploadFilePath = null;
    private static String downloadFilePath = null;
    
    private static ServletContext application;
    private static WebApplicationContext webApplicationContext;
    
    /**
     * 上传文件的路径,末尾带/
     * @return
     */
    public final static String getUploadFilePath(){
        return uploadFilePath;
    }
    
    /**
     * 下载文件目录,末尾带 /
     * @return
     */
    public final static String getDownloadFilePath(){
        return downloadFilePath;
    }
    /**
     * 得到 webRoot 的绝对目录,末尾带/
     * @return
     */
    public final static String getWebRoot(){
        return webRoot;
    }
    
    /**
     * 得到 WEB-INF 的绝对目录,末尾带/
     * @return
     */
    public final static String getWebInf(){
        return webInf;
    }
    
    /**
     * 得到系统 application 对象
     * @return
     */
    public final static ServletContext getApplication(){
        return application;
    }
    
    /**
     * 得到完整的带参数的当前地址
     * @param req
     * @return
     */
    public final static String getFullUrl(HttpServletRequest req){
        String url=req.getRequestURL().toString();    
        if(req.getQueryString()!=null && req.getQueryString().trim().length()>0){
            url += "?"+req.getQueryString();
        }
        return url;
    }
    /**
     * 得到URL的根, 末尾带/
     * 
     * @param req
     * @return
     */
    public final static String getURLRoot(HttpServletRequest req){
        String key = "FRAME_REQUEST_URL_ROOT";
        if(req.getAttribute(key) !=null) {
            return (String)req.getAttribute(key);
        }
        
        String url=req.getRequestURL().toString();    
        String uri = req.getRequestURI();
        String root = req.getContextPath();
        
        String head = "";
        int pos = 0;
        if(uri.equals("/")){
            pos = url.indexOf(uri,url.indexOf("//")+2);
        }else{
            pos = url.indexOf(uri);
        }
        head = url.substring(0,pos);
        
        if(!head.endsWith("/")){
            head += "/";
        }
        if(!root.equals("")){
            if(root.startsWith("/")){
                root = root.substring(1);
            }
            head += root;
        }
        if(! head.endsWith("/")){
            head = head + "/";
        }
        
        req.setAttribute(key, head);
        return head;
    }
 
    static Logger log = Logger.getLogger(FrameUtil.class);
    
    protected static void setDownloadFilePath(String path){
 
        if(path ==null || path.trim().length()==0){
            path = FrameUtil.getWebRoot()+"WEB-INF/upload";
            new File(path).mkdirs();
            downloadFilePath = path + "/";
            return;
        }
        
        path = path.trim();
        if(path.endsWith("\\") || path.endsWith("/")){
            path = path.substring(0, path.length()-1);
        }
 
        File f = new File(path);
        if(f.exists() && f.isDirectory()){
            downloadFilePath = path + "/";
            return;
        }
    
        if(path.startsWith("/") || path.startsWith("\\")){
            path = path.substring(1);
        }
        
        f = new File(FrameUtil.getWebRoot() + path);
        if(f.exists() && f.isDirectory()){
            downloadFilePath = FrameUtil.getWebRoot() + path + "/";
            return;
        }
        
        f = new File(FrameUtil.getWebInf() + path);
        if(f.exists() && f.isDirectory()){
            downloadFilePath = FrameUtil.getWebInf() + path + "/";
            return;
        }
        // fuzhen
        
        downloadFilePath = "D:\\";
                
                return ;
        //throw new RuntimeException(
            //    "必须在 framework.startup.FrameStartup 中指定文件上传路径");
    }
    protected static void setUploadFilePath(String path){
 
        if(path ==null || path.trim().length()==0){
            path = FrameUtil.getWebRoot()+"WEB-INF/upload";
            new File(path).mkdirs();
            uploadFilePath = path + "/";
            return;
        }
        
        path = path.trim();
        if(path.endsWith("\\") || path.endsWith("/")){
            path = path.substring(0, path.length()-1);
        }
 
        File f = new File(path);
        if(f.exists() && f.isDirectory()){
            uploadFilePath = path + "/";
            return;
        }
    
        if(path.startsWith("/") || path.startsWith("\\")){
            path = path.substring(1);
        }
        
        f = new File(FrameUtil.getWebRoot() + path);
        if(f.exists() && f.isDirectory()){
            uploadFilePath = FrameUtil.getWebRoot() + path + "/";
            return;
        }
        
        f = new File(FrameUtil.getWebInf() + path);
        if(f.exists() && f.isDirectory()){
            uploadFilePath = FrameUtil.getWebInf() + path + "/";
            return;
        }
        
        // fuzhen
        
        uploadFilePath = "D:\\";
        
        return ;
        
        //throw new RuntimeException(
                //"必须在 framework.startup.FrameStartup 中指定文件上传路径");
    }
    
    protected static void setServletContext(ServletContext app) {
        if(app ==null){
            throw new RuntimeException("NULL ServletContext");
        }
 
        application = app;
        
        webRoot = application.getRealPath("/").trim();
        if(! (webRoot.endsWith("/") || webRoot.endsWith("\\"))){
            webRoot = webRoot + "/";
        }
        
        log.info("webRoot: "+webRoot);
        
        webInf = webRoot + "WEB-INF/";
        log.info("WEB-INF: "+webInf);
        
    }
    
    protected static void setWebApplicationContext(WebApplicationContext context){
        webApplicationContext = context;
    }
    
    /**
     * 得到spring bean
     * @param beanId
     * @return
     */
    public static Object getBean(String beanId){
        return webApplicationContext.getBean(beanId);
    }
    
//    DataSourceTransactionManager txManager = (DataSourceTransactionManager)wac.getBean("txManager");
//    
//    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
//    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
//    TransactionStatus status = txManager.getTransaction(def);
//    DefaultSqlSessionFactory sf  = (DefaultSqlSessionFactory)wac.getBean("sqlSessionFactory");
//    SqlSession sess = sf.openSession(false);
//
//    UserMapper mapper = sess.getMapper(UserMapper.class);
//    
//    TSysUser user = new TSysUser();
//    user.setUserId("40733b24-c5c2-11e5-b84d-6a9eda6d908a");
//    user.setRealName("aaaaaaa");
//    
//    mapper.updateUser(user);
//    
//    //txManager.rollback(status);
//    txManager.commit(status);
//    sess.close();
 
}